11.SSM集成

 

项目目录结构:

项目依赖包(31个jar):

UserVo

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.course.vo;

import java.util.Date;

public class UserVo {
private Integer id;

private String name;

private Integer age;

private String address;

private Date hireDate;

private Integer pageIndex;
private Integer pageSize;

public UserVo() {
super();
}

public UserVo(String name, Integer age, String address, Date hireDate) {
super();
this.name = name;
this.age = age;
this.address = address;
this.hireDate = hireDate;
}

public UserVo(Integer id, String name, Integer age, String address, Date hireDate) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.hireDate = hireDate;
}

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 == null ? null : name.trim();
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address == null ? null : address.trim();
}

public Date getHireDate() {
return hireDate;
}

public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}

public Integer getPageIndex() {
return pageIndex;
}

public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
}

public Integer getPageSize() {
return pageSize;
}

public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}

@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + ", hireDate=" + hireDate
+ "]";
}

}

util: PageBean.java

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.util;

public class PageBean {

private Integer pageIndex;
private Integer pageSize;
private Integer totalPage;
private Integer totalCount;

public Integer getPageIndex() {
return pageIndex;
}
public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
this.totalPage = (int) Math.ceil((this.totalCount*1.0)/this.pageSize);
}

}

dao: UserMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.course.dao;

import java.util.List;

import com.course.vo.UserVo;

public interface UserMapper {
int deleteByPrimaryKey(Integer id);

int insert(UserVo record);

int insertSelective(UserVo record);

UserVo selectByPrimaryKey(Integer id);

int updateByPrimaryKeySelective(UserVo record);

int updateByPrimaryKey(UserVo record);

List<UserVo> getAllUser();

List<UserVo> getUserByVo(UserVo user);

}

mapper.xml: UserMapper.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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.course.dao.UserMapper">
<resultMap id="BaseResultMap" type="com.course.vo.UserVo">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="hire_date" jdbcType="TIMESTAMP" property="hireDate" />
</resultMap>
<sql id="Base_Column_List">
id, name, age, address, hire_date
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.course.vo.UserVo">
insert into user (id, name, age,
address, hire_date)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{address,jdbcType=VARCHAR}, #{hireDate,jdbcType=TIMESTAMP})
</insert>
<insert id="insertSelective" parameterType="com.course.vo.UserVo">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="age != null">
age,
</if>
<if test="address != null">
address,
</if>
<if test="hireDate != null">
hire_date,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="address != null">
#{address,jdbcType=VARCHAR},
</if>
<if test="hireDate != null">
#{hireDate,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.course.vo.UserVo">
update user
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
<if test="address != null">
address = #{address,jdbcType=VARCHAR},
</if>
<if test="hireDate != null">
hire_date = #{hireDate,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.course.vo.UserVo">
update user
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
address = #{address,jdbcType=VARCHAR},
hire_date = #{hireDate,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
</update>

<select id="getAllUser" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
</select>

<select id="getUserByVo" parameterType="com.course.vo.UserVo" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
</select>

</mapper>

service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.course.service;

import java.util.List;

import com.course.util.PageBean;
import com.course.vo.UserVo;

public interface UserService {

public void addUser(UserVo user);
public void updateUser(UserVo user);
public void deleteUser(UserVo user);

public UserVo queryUserById(Integer id);

public List<UserVo> queryAllUser();

public List<UserVo> queryUserList(PageBean pageBean, UserVo 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.course.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.course.dao.UserMapper;
import com.course.service.UserService;
import com.course.util.PageBean;
import com.course.vo.UserVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;

public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}

@Override
public void addUser(UserVo user) {
userMapper.insertSelective(user);
}

@Override
public void updateUser(UserVo user) {
userMapper.updateByPrimaryKey(user);
}

@Override
public void deleteUser(UserVo user) {
userMapper.deleteByPrimaryKey(user.getId());
}

@Override
public UserVo queryUserById(Integer id) {
UserVo user = userMapper.selectByPrimaryKey(id);
return user;
}

@Override
public List<UserVo> queryAllUser() {
List<UserVo> userList = userMapper.getAllUser();
return userList;
}

@Override
public List<UserVo> queryUserList(PageBean pageBean, UserVo user) {
Page<UserVo> page = PageHelper.startPage(pageBean.getPageIndex(), pageBean.getPageSize());
List<UserVo> userList = userMapper.getUserByVo(user);
pageBean.setTotalCount((int)page.getTotal());;
return userList;
}

}

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.course.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.course.service.UserService;
import com.course.util.PageBean;
import com.course.vo.UserVo;

@Controller
@RequestMapping("user")
public class UserController {

@Autowired
private UserService userService;

public void setUserService(UserService userService) {
this.userService = userService;
}

public void addUser() {
Date hireDate = new Date();
UserVo user = new UserVo("name2", 2, "address2", hireDate);
userService.addUser(user);
}

public void updateUser() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date hireDate = null;
try {
hireDate = simpleDateFormat.parse("2022-01-01 08:30:20");
} catch (ParseException e) {
e.printStackTrace();
}
UserVo user = new UserVo(168, "name2", 12, "address2", hireDate);
userService.updateUser(user);
}

public void deleteUser() {
UserVo user = new UserVo();
user.setId(172);
userService.deleteUser(user);
}

public UserVo queryUserById(Integer id) {
return userService.queryUserById(id);
}

public List<UserVo> queryAllUser() {
return userService.queryAllUser();
}

@RequestMapping("queryUserList")
public String queryUserList(UserVo userVo, Model model) {
PageBean pageBean = new PageBean();
if (userVo.getPageIndex() == null) {
// pageIndex从1开始,不是从0开始
pageBean.setPageIndex(1);
}
else {
pageBean.setPageIndex(userVo.getPageIndex());
}
if (userVo.getPageSize() == null) {
pageBean.setPageSize(5);;
}
else {
pageBean.setPageSize(userVo.getPageSize());
}
List<UserVo> userList = userService.queryUserList(pageBean, userVo);
model.addAttribute("userList", userList);
model.addAttribute("pageBean", pageBean);
return "userList";
}

}

log4j.properties

1
2
3
4
5
6
7
8
# Global logging configuration
log4j.rootLogger=DEBUG, 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

db.properties

1
2
3
4
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/springmvc_test
username=root
password=123456

mybatis.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<!-- MyBatis的整体配置 -->
<configuration>

<settings>
<!-- 让log4j去记录sql日志,打印mybatis执行的sql语句 -->
<setting name="logImpl" value="LOG4J"/>
<!-- https://mybatis.org/mybatis-3/zh/configuration.html#properties -->
</settings>

<!-- 使用plugin: PageHelper,不能在application-dao.xml中注入,必须在mybatis.cfg.xml中添加插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

</configuration>

springmvc.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
35
36
37
38
<?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"
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">


<!--
配置控制器
包扫描
-->
<context:component-scan base-package="com.course.controller"></context:component-scan>

<!--
合并注解的控制器映射器和控制器适配器的配置
对控制器映射器和控制器适配器进行加强,如把对象转化为json字符串
-->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/WEB-INF/view/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

application-dao.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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">


<!-- 解析配置文件 -->
<!--
${username}在Spring里默认取的是当前的主机名,
如果想禁用取主机名可以使用system-properties-mode="FALLBACK"
-->
<context:property-placeholder location="classpath:db.properties" system-properties-mode="FALLBACK"/>

<!-- 声明数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 注入相关属性 -->
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>

<!-- 声明sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 注入mybatis.cfg.xml -->
<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>


<property name="mapperLocations">
<array>
<value>classpath:com/course/mapper/*.xml</value>
</array>
</property>

<!-- 使用plugin: PageHelper,不能在application-dao.xml中注入,必须在mybatis.cfg.xml中添加插件 -->
<!--
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor"></bean>
</array>
</property>
-->

</bean>

<!--
配置接口扫描
因为UserMapper.java没有实现类,所以必须依靠cglib在内存中构造代理对象
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入mapper接口所在的包 -->
<property name="basePackage" value="com.course.dao"></property>

<!--
如果有多个mapper接口,value可以为数组,用逗号或者换行分割
<property name="basePackage" value="com.course.dao,com.course.dao2"></property>

<property name="basePackage">
<value>
com.course.dao
com.course.dao2
</value>
</property>
-->

<!--
注入sqlSessionFactory
因为是BeanName,所以是value,不是ref
-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

</beans>

application-service.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">


<!-- 声明式事务的配置开始 -->
<!-- 1.声明事务管理器 -->
<!-- 用Spring自带的事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 2.声明通知方式 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<!-- 配置那些方法要加入事务 -->
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 3.进行AOP配置 -->
<aop:config>
<!-- 声明切面 -->
<!-- 事务应该加在service层,不应该加在dao层 -->
<aop:pointcut id="pc" expression="execution(* com.course.service.impl.*.*(..))"/>
<!-- 织入 -->
<aop:advisor advice-ref="advice" pointcut-ref="pc"/>
</aop:config>
<!-- 声明式事务的配置结束 -->

<!-- 声明Service -->
<!-- <bean id="userService" class="com.course.service.impl.UserServiceImpl" autowire="byType"></bean> -->

<context:component-scan base-package="com.course.service"></context:component-scan>

</beans>

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">


<import resource="classpath:application-dao.xml"/>
<import resource="classpath:application-service.xml"/>

</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
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
61
62
63
64
65
<?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>05_ssm</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>

<!-- 配置SpringMVC编码过滤器 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<!--
方法1: <url-pattern>*.do</url-pattern>
<url-pattern>/*</url-pattern>
-->
<!-- 指定某个servlet去过滤 -->
<servlet-name>springmvc</servlet-name>
</filter-mapping>

<!-- 配置Spring的监听器,在项目启动时加载applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置applicationContext.xml的地址 -->
<context-param>
<!-- ContextLoaderListener的父类ContextLoader的属性 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- 配置前端控制器 -->
<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>

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h1>
<a href="user/queryUserList.do">click here to query user information</a>
</h1>
</body>
</html>

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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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>
<th>HireDate</th>
<th>Avg</th>
<tr>
</thead>
<tbody>
<c:choose>
<c:when test="${empty userList }">
<tr>
<td algin="center" colspan="5">
no result
</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>
<fmt:formatDate value="${user.hireDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
</td>
<td>
<fmt:formatNumber value="2.35345" pattern="0.000 times"></fmt:formatNumber>
</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
<tr>
<td algin="right" colspan="5">
<c:choose>
<c:when test="${pageBean.pageIndex==1 }">
previous
</c:when>
<c:otherwise>
<a href="javascript:void(0)" onclick="changePage(${pageBean.pageIndex-1})">previous</a>
</c:otherwise>
</c:choose>
&nbsp;&nbsp;&nbsp;
<c:forEach begin="1" end="${pageBean.totalPage }" var="pageNum">
<c:choose>
<c:when test="${pageBean.pageIndex==pageNum }">
${pageNum }
</c:when>
<c:otherwise>
<a href="javascript:void(0)" onclick="changePage(${pageNum})">${pageNum }</a>
</c:otherwise>
</c:choose>
</c:forEach>
&nbsp;&nbsp;&nbsp;
<c:choose>
<c:when test="${pageBean.pageIndex==pageBean.totalPage }">
next
</c:when>
<c:otherwise>
<a href="javascript:void(0)" onclick="changePage(${pageBean.pageIndex+1})">next</a>
</c:otherwise>
</c:choose>
&nbsp;&nbsp;&nbsp;
total ${pageBean.totalPage } page
&nbsp;&nbsp;&nbsp;
Jump to
<select onchange="jumpToPage(this)">
<c:forEach begin="1" end="${pageBean.totalPage }" var="pageNum">
<c:choose>
<c:when test="${pageBean.pageIndex==pageNum }">
<option value="${pageNum }" selected="selected">${pageNum }</option>
</c:when>
<c:otherwise>
<option value="${pageNum }">${pageNum }</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
page
</td>
</tr>
</tbody>
</table>
</body>
<script type="text/javascript">
function changePage(pageIndex) {
window.location.href="queryUserList.do?pageIndex="+pageIndex;
}
function jumpToPage(obj) {
var pageNum = obj.value;
changePage(pageNum);
}
</script>
</html>

浏览器访问: