一个简单的HelloSpringMVC程序

先在web,xml中注册一个前端控制器(DispatcherServlet)

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置DispatcherServlet:这是SpringMVC的核心,请求分发器,前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--要绑定springmvc的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别为1-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

配置文件(springmvc-servlet.xml)

HandlerMapper是处理器映射器-->根据请求的地址去找处理器(如案例中的"/hello")

HandlerAdapter是处理器适配器-->找到处理器后根据id去适配对应的controller(如适配到案例中的HelloController),controller会返回ModelAndView及其前端数据

ViewResolver是视图解析器,其作用为:

1.获取到ModelAndView中的数据

2.解析视图名称

3.拼接视图名称

4.数据渲染

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> <bean id="/hello" class="com.kuang.contorller.HelloController"/> </beans>

controller层:

实现Controller接口,重写内部方法(一般不会使用,这是底层原理)

ModelAndView是模型、视图

 public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","HelloSpringMVC!");
mv.setViewName("test");
return mv;
}
}

底层流程图

实线是SpringMVC已经帮你实现好了,虚线是需要自己手动

SpringMVC底层执行原理-LMLPHP

以上仅是说明底层执行原理,实际开发并不会这样去使用!

在实际开发中SpringMVC推荐使用注解的方式

在注解开发中,不需要我们去配置处理器适配器和处理器映射器。

web.xml中只需配置DispatcherServlet前端控制器

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <servlet>
<servlet-name>SpringMVC</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>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

在springmvc-servlet.xml中配置视图解析器等

<context:component-scan base-package="com.kuang.controller"/>    /*组件扫描,用于扫描controller下的包*/
<mvc:default-servlet-handler/> /*静态资源过滤*/
<mvc:annotation-driven/> /*这个就帮助我们配置了映射器以及适配器*/
 <?xml version="1.0" encoding="UTF-8"?>
<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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.kuang.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

contorller

 @Controller    //说明这类被Spring托管了
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/h1")   //这个注解会执行视图解析器
public String hello(Model model){
model.addAttribute("msg","helloSpringMVCAnnotation");
return "hello";
}
}
05-12 09:17