Basic configuration of spring MVC

Record the steps to build the spring MVC framework

I. create a new Java Web project, springmvc1, and import the jar package in lib

This project uploads GitHub, which is convenient to download jar package

https://github.com/yeyangtao/springmvc

2. web.xml file under Web inf

Among them, serve name's spingmvc will be used in the next step.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4">
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

3. Create springmvc-servlet.xml in the same path

The spring MVC in the filename corresponds to the spring MVC in the servlet name in the previous step

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="simpleUrlHandlerMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/hello">indexController</prop>
            </props>
        </property>
    </bean>
    <bean id="indexController" class="controller.IndexController"></bean>
</beans>

/hello specifies the path entered on the waiting browser

IV. create a new IndexController class in src to implement the Controller interface

package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
 
public class IndexController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("index.jsp");
        mav.addObject("message", "Hello Spring MVC");
        return mav;
    }
}

The model in ModelAndView represents the model and view represents the view. This name explains the function of this class well. After the business processor calls the model layer to process the user's request, it stores the result data in the model attribute of the class, stores the view information to be returned in the view attribute of the class, and then returns the model and view to the Spring MVC framework. The framework parses the object by calling the view parser defined in the configuration file, and finally displays the result data on the specified page (index.jsp).

V. index.jsp is used to display

Use the el expression ${message}

Vi. deploy to tommcat for testing

Browser input http://localhost:8080/springmvc1/hello

Result:

Tags: Java xml Spring JavaEE

Posted on Sat, 09 Nov 2019 10:45:44 -0500 by Saeven