Spring MVC
1.1、Hello Spring MVC
1.1.1、原理图
1. 用户访问 /index
2. 根据web.xml中的配置 所有的访问都会经过DispatcherServlet
3. 根据 根据配置文件springmvc-servlet.xml ,访问路径/index
会进入IndexController类
4. 在IndexController中指定跳转到页面index.jsp,并传递message数据
5. 在index.jsp中显示message信息
![](https://img-blog.csdn.net/20180304104835298?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmluaXNoX2RyZWFt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
1.1.2、web.xml
在WEB-INF目录下创建web.xml
配置Spring MVC的入口 DispatcherServlet,把所有请求都提交到该Servlet
注意:<servlet-name>springmvc</servlet-name>
springmvc这个名字在下一步会用到
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="。。。">
<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>
1.1.3、创建springmvc-servlet.xml
在WEB-INF目录下创建springmvc-servlet.xml
springmvc-setvlet.xml与上一步中的
<sevlet-name>springmvc</servlet-name>
springmvc对应
这是Spring MVC的 映射配置文件
表示访问路径/index会交给id=indexController的bean处理
id=indexController的bean配置为类:IndexController
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "。。。">
<beans>
<bean id="simpleUrlHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index">indexController</prop>
</props>
</property>
</bean>
<bean id="indexController" class="controller.IndexController"></bean>
</beans>
1.1.4、控制类indexController
控制类IndexController实现接口Controller,提供方法handleRequest请求
SpringMVC通过ModelAndView对象把模型和视图结合在一起
ModelAndView mav = new ModelAndView("index.jsp");
mav.addObject("message", "Hello Spring MVC");
表示视图是index.jsp
模型数据是 message,内容是 “Hello Spring MVC”
。。。
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;
}
}
1.1.5、准备index.jsp
index.jsp很简单,通过EL表达式显示message的内容
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<h1>${message}</h1>
1.2、视图定位
如果代码写成这样,就表示跳转到页面index.jsp
new ModelAndView("index.jsp");
所谓的视图定位,指的是代码还是写成这样,但是会跳转到/WEB-INF/page/index.jsp
new ModelAndView("index");
1.2.1、修改springmvc-servlet.xml
修改springmvc-servlet.xml,增加
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
其作用是把视图定位在/WEB-INF/page/*.jsp这个位置
<?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="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="simpleUrlHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index">indexController</prop>
</props>
</property>
</bean>
<bean id="indexController" class="controller.IndexController"></bean>
</beans>
1.2.2、修改IndexController
把IndexController类的这一行代码
ModelAndView mav = new ModelAndView("index.jsp");
修改为
ModelAndView mav = new ModelAndView("index");
public class IndexController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("index");
mav.addObject("message", "Hello Spring MVC");
return mav;
}
}
1.2.3、移动index.jsp
在WEB-INF下新建目录page
把index.jsp移动到 WEB-INF/page 目录下
![](https://img-blog.csdn.net/20180304104844652?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmluaXNoX2RyZWFt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
1.3、注解方式
前面的例子,都是使用配置的方式进行跳转的配置,本例讲解如何使用注解的方式进行跳转的配置
1.3.1、修改IndexController
在类前面加上@Controller表示该类是一个控制器
在方法handleRequest前面加上@RequestMapping("index")表示路径/index会映射到该方法上
注意:不再让IndexController实现Controller接口
。。。
@Controller
public class IndexController {
@RequestMapping("/index")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("index");
mav.addObject("message", "Hello Spring MVC");
return mav;
}
}
1.3.2、修改springmvc-servlet.xml
去掉映射相关的配置,因为已经使用注解方式了
增加
<context:component-scan base-package="controller" />
表示从包controller下扫描有@Controller注解的类