4.SpringMVC映射器和适配器配置(xml)

 

项目工程目录:

项目依赖包:

Vo: User

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.course.vo;

public class User {

private Integer id;
private String name;
private Integer age;

public User() {
super();
}

public User(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.course.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.course.vo.User;

public class UserController1 implements Controller {

@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
List<User> userList = new ArrayList<>();
User user;
for (int i = 0; i < 10; i++) {
user = new User(i, "name" + i, 10 + i);
userList.add(user);
}

// 封装页面地址和发送到页面的数据
ModelAndView modelAndView = new ModelAndView();

// 相当于httpServletRequest.setAttribute();
modelAndView.addObject("userList", userList);

// 相当于httpServletRequest.getRequestDispatcher("WEB-INF/view/UserList.jsp").forward(httpServletRequest, httpServletResponse);
modelAndView.setViewName("WEB-INF/view/UserList.jsp");

return modelAndView;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.course.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.HttpRequestHandler;

import com.course.vo.User;

public class UserController2 implements HttpRequestHandler {

@Override
public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
List<User> userList = new ArrayList<>();
User user;
for (int i = 0; i < 5; i++) {
user = new User(i, "name" + i, 10 + i);
userList.add(user);
}

// 封装页面地址和发送到页面的数据
httpServletRequest.setAttribute("userList", userList);
httpServletRequest.getRequestDispatcher("WEB-INF/view/UserList.jsp").forward(httpServletRequest, httpServletResponse);

}
}

springmvc.xml

处理器映射器:

1.BeanNameUrlHandlerMapping: 把bean的名字做为url进行查找,也就是handler必须配置name属性。这样配置简单 但是每一个Controller只能指向一个url

2.SimpleUrlHandlerMapping: 如果一个Controller要指向多个url,就必须使用SimpleUrlHandlerMapping

处理器适配器:

1.SimpleControllerHandlerAdapter: controller实现Controller接口

2.HttpRequestHandlerAdapter: controller实现HttpRequestHandler接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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">


<!--
配置控制器
name属性: 其实就是页面请求过来的url
-->
<bean id="userController1" name="/getUserList1.do" class="com.course.controller.UserController1"></bean>
<bean id="userController2" name="/getUserList2.do" class="com.course.controller.UserController2"></bean>

<!--
配置控制器映射器
有两个处理器映射器,这两个处理器映射器可以共存:
1:org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping把bean的名字做为url进行查找,也就是handler必须配置name属性
2:org.springframework.web.servlet.handler.SimpleUrlHandlerMapping 里面可以配置属性
-->
<!-- controller必须有name属性,通过name属性值查找对应的controller -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!--
key: 页面请求的url
value: controller的bean的id
-->
<prop key="/getUserList10.do">userController1</prop>
<prop key="/getUserList11.do">userController1</prop>
<prop key="/getUserList20.do">userController2</prop>
<prop key="/getUserList21.do">userController2</prop>
</props>
</property>
</bean>

<!--
配置控制器适配器
有两个处理器适配器,这两个处理器适配器可以共存:
1:org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter写的controller要实现Controller的接口
2:org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter 写controler要实现HttpRequestHandler接口
-->
<!-- controller实现Controller接口 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>

<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

</beans>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>01_springmvc_hello</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>

<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 加载springmvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>

<!-- 启动加载 -->
<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>

UserList.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>User List</h1>
<hr/>
<table align="center" width="60%" border="1" cellspacing="5">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<tr>
</thead>
<tbody>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>

log4j.properties

1
2
3
4
5
6
7
8
# Global logging configuration
log4j.rootLogger=INFO, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

启动Tomcat

浏览器访问:

1
2
3
http://localhost:8080/01_springmvc_init/getUserList1.do
http://localhost:8080/01_springmvc_init/getUserList10.do
http://localhost:8080/01_springmvc_init/getUserList11.do

浏览器访问:

1
2
3
http://localhost:8080/01_springmvc_init/getUserList2.do
http://localhost:8080/01_springmvc_init/getUserList20.do
http://localhost:8080/01_springmvc_init/getUserList21.do