简单的实例
学习了SpringMVC 的整体架构,SpringMVC应用开发一般包括以下几步
- 配置web.xml,指定业务层对应的Spring配置文件,定义DispatcherServlet(也可以编程配置)
- 编写处理请求的控制器
- 编写视图对象
- 配置SpringMVC的配置文件,使控制器 、视图解析器等生效
以下是项目结构
1.1 web.xml
上一页已经很详细的讲解了
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appcationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>*.html</url-pattern>
</servlet-mapping>
</web-app>
1.2.Java配置 与web.xml 配置等效
package com.config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class SpringmvcApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("/WEB-INF/springmvc-servlet.xml");
ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(appContext));
dynamic.setLoadOnStartup(1);
dynamic.addMapping("*.html*");
}
}
- 编写处理请求的控制器
package com.sjy;
@Controller // 使用Controller 注解将一个POJO转化为处理请求的控制器
@RequestMapping("/user")//通过RequestMapping为控制器指定处理那些URL的请求
public class UserController {
@RequestMapping("/register")
public String register(){
return "register";
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView user(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("c");
modelAndView.addObject("sjy","天天向上");
return modelAndView;
}
@RequestMapping("/s")
@ResponseBody
public String s(){
return "register";
}
}
3.视图对象
register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加用户</title>
</head>
<body>
<div>
<%-- 用于跳转路径 --%>
<a href="${pageContext.request.contextPath}/user.html">sssss</a>
</div>
</body>
</html>
c.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8"%>
<html>
<head>
<title>成功</title>
</head>
<body>
<div>
恭喜,用户${sjy} ,嘻嘻
</div>
</body>
</html>
- 配置Spring MVC的配置文件
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--使Spring扫描com.sjy包下的所有类,让标注Spring注解的类生效-->
<context:component-scan base-package="com.sjy"/>
<!--定义了一个视图名称解析器,将视图逻辑名解析为/WEB_INF/views/<viewsName.jsp> -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp"/>
</beans>
tomcat运行
回顾SpringMVC处理请求的整个过程
- DispatcherServlet接受到客户端的/user.html请求
- DispatcherServlet使用DefaultAnnotationHandlerMapping查找负责处理该请求的处理器
- DispatcherServlet将请求分发给名为/user.html的UserController处理器。
- 处理器完成业务处理后,返回ModelAndView对象,其中View名字为c,包含一个键位sjy的字符串。
- DispatcherServlet调用InternalResourceViewResolver组件,对ModelAndView中的逻辑视图名进行解析,得到真实的/WEB-INF/views/c.jsp视图对象。
- DispatcherServlet使用/WEB-INF/views/c.jsp对模型的中的”天天向上“进行渲染。
- 返回响应