1.SpringMVC和struts2都属于表现层的框架,它是Spring框架的一部分,SpringMVC是Spring框架的一个模块。整体结构如下
2.SpringMVC框架
–.前端控制器 DispatcherServlet
作用:接受请求参数,相应结果,相当于转发器,不需要程序员开发,有了DispatcherServlet减少了其他组件之间的耦合度;
–处理器映射器 HandlerMapping
作用:根据请求的url查找Handler
–处理器适配器HandlerAdapter
作用:按照特定规则去执行Handler
–处理器Handler
作用:编写时按照HandlerAdapter的要求,这样适配器才能正确执行Handler
–.视图解析器ViewResolver
作用:进行视图解析,根据逻辑视图名解析成真正的视图
3.搭建环境
-》导入jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.10.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
-》配置前端控制器(web.xml)
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- springmvc前端控制器 -->
<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.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 解决post传参乱码问题 -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
-》配置处理器适配器
<?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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.mvc"/>
<!--配置注解的映射器和适配器-->
<mvc:annotation-driven/>
<!--处理器映射器(查找Handler)-->
<!--
<bean name="/user.action" class="com.mvc.controller.UserController"></bean>
-->
<!-- 配置处理器适配器(执行Handler,并返回ModelAndView) -->
<!--
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
-->
<!-- 配置视图解析器,解析jsp,默认使用jstl标签,需要导入jstl的jar包-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--设置前缀和后缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
-》编写实体类User,UserEx
public class User {
private int id;
private String name;
private int age;
。。。
}
public class UserEx {
private int eid;
private User user;
。。。
}
-》编写Handler(UserController)
@Controller
public class UserController{
//当请求为show.action,并且是POST请求时,才会执行下面的方法
@RequestMapping(value = "/show.action",method = RequestMethod.POST)
public ModelAndView show(UserEx user){
List<UserEx> list = new ArrayList<>();
list.add(user);
System.out.println(list.toString());
ModelAndView mav = new ModelAndView();
mav.addObject("userList", list);
mav.setViewName("user");
return mav;
}
//可以选择使用重定向或者转发
@RequestMapping("/show2.action")
public void Show2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<UserEx> list = new ArrayList<>();
list.add(new UserEx(1,new User(2,"?",15)));
System.out.println(list.toString());
request.setAttribute("userList",list);
request.getRequestDispatcher("/WEB-INF/jsp/user.jsp").forward(request,response);
}
//和show1类似,需要使用Model类保存信息
@RequestMapping("/show3.action")
public String show3(Model model){
List<UserEx> list = new ArrayList<>();
list.add(new UserEx(3,new User(4,"✋",45)));
System.out.println(list.toString());
model.addAttribute("userList", list);
return "user";
}
//Rest风格
@RequestMapping("/show4/{id}/{name}.action")
public String show4(@PathVariable int id, Model model,@PathVariable String name) {
System.out.println(id);
List<UserEx> list = new ArrayList<>();
list.add(new UserEx(3,new User(4,"?",45)));
System.out.println(list.toString());
model.addAttribute("userList", list);
return "user";
}
}
-》测试
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="show.action" method="post">
id:<input type="text" name="eid"/><br>
ID:<input type="text" name="user.id"/><br>
Name:<input type="text" name="user.name"/><br>
Age:<input type="text" name="user.age"></br>
<input type="submit"value="提交">
</form>
<a href="${pageContext.request.contextPath}/show4/4396/tom.action">伪静态</a>
</body>
</html>
user.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>用户信息</h1>
<hr>
<table border="1" width="80%" align="center">
<tr>
<td>id</td>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.eid}</td>
<td>${user.user.id}</td>
<td>${user.user.name}</td>
<td>${user.user.age}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
web目录