SpringMVC的简单流程

  1. 引入springmvc相关的jar包。

   

 

 

  2. 配置DispatcherServlet web.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringMVC_9.03</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <!-- 编码设置 -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 配置DispatcherServlet -->
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:SpringMVC-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 

 

  3. 配置springmvc的配置文件。<?xml version="1.0" encoding="UTF-8"?>

    如果DispatcherServlet 处理的请求地址为/,那么它也会处理静态资源。

  在配置文件中使用<mvc:default-servlet-handler>不处理静态资源

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
   <!-- 包扫描:扫描注解所在的包controller类所在的包 -->
    <context:component-scan base-package="com.zhiyou100.hhz.controller"></context:component-scan>
  
   <!-- 开启注解驱动AnnotationHandlerMapping -->
   <mvc:annotation-driven/>
   <!-- 静态资源释放 -->
   <mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>

 

  4.  编写控制器Controller

@Controller
@RequestMapping("user")
@SessionAttributes(names= {"name"})
public class UserController {

    @RequestMapping("login.do")
    public String login(User user) {      //User封装的实体类
        System.out.println(user.getName());
        System.out.println(user.getPassword());
        return "index";
    }
}

 

  

 

 

 

  具体实现步骤

  1. 由网页发出请求。http://localhost:8080/springmvc-01/user/login.do.

  2. 进入web.xml文件中DispatcherServlet.查看是否符合url-pattern的要求

  3. 然后进入springmvc的配置文件。找HandlerMapping.

  4. 找到我的MyController类。执行该类中handleRequestInternal方法。

  5.根据返回的modelAndView在找springmvc配置文件中视图解析器。

  6.最后viewNameprefix以及suffix做了一个拼接。把拼接的页面展示给客户。