首页 > 代码库 > SSM项目整合

SSM项目整合

SSM项目整合基本步骤

SSM项目整合

1、基本概念

1.1、Spring

  Spring 是一个开源框架, Spring 是于 2003  年兴起的一个轻量级的 Java  开发框架,由 Rod Johnson  在其著作 Expert One-On-One J2EE Development and Design 中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。 Spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情。然而, Spring 的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。 简单来说, Spring 是一个轻量级的控制反转( IoC )和面向切面( AOP )的容器框架。

1.2、SpringMVC

       Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring Web Flow 里面。 Spring MVC  分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

1.3、MyBatis

       MyBatis  本是 Apache 的一个开源项目iBatis , 2010 年这个项目由 apache software foundation  迁移到了 google code ,并且改名为 MyBatis  。 MyBatis 是一个基于 Java 的 持久层框架。 iBATIS 提供的持久层框架包括 SQL Maps 和 Data Access Objects ( DAO ) MyBatis  消除了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。 MyBatis  使用简单的  XML 或注解用于配置和原始映射,将接口和  Java  的 POJOs ( Plain Old Java Objects ,普通的  Java 对象)映射成数据库中的记录。

2、SSM 项目整合——系统结构图

技术分享

3、SSM项目结构搭建

3.1、需要的Jar包

  ①Spring:

    spring-aop-4.3.0.RELEASE.jar
    spring-aspects-4.3.0.RELEASE.jar
    spring-beans-4.3.0.RELEASE.jar
    spring-context-4.3.0.RELEASE.jar
    spring-context-support-4.3.0.RELEASE.jar
    spring-core-4.3.0.RELEASE.jar
    spring-expression-4.3.0.RELEASE.jar
    spring-jdbc-4.3.0.RELEASE.jar
    spring-tx-4.3.0.RELEASE.jar
    spring-web-4.3.0.RELEASE.jar
    spring-webmvc-4.3.0.RELEASE.jar

    spring-webmvc-portlet-4.3.0.RELEASE.jar
    spring-websocket-4.3.0.RELEASE.jar

    com.springsource.net.sf.cglib-2.2.0.jar
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    commons-logging-1.2.jar

  ③MyBatis:

    mybatis-3.1.1.jar

    log4j-1.2.16.jar
    mybatis-spring-1.2.1.jar

  ④文件上传/下载:

    commons-fileupload-1.2.2.jar
    commons-io-2.4.jar

  ⑤C3P0数据源:

    c3p0-0.9.5.1.jar

    mchange-commons-java-0.2.10.jar

    classmate-0.8.0.jar

  ⑥MySQL:

    mysql-connector-java-5.0.8-bin.jar

  ⑦JSTL:

    taglibs-standard-compat-1.2.5.jar
    taglibs-standard-impl-1.2.5.jar
    taglibs-standard-jstlel-1.2.5.jar
    taglibs-standard-spec-1.2.5.jar

  ⑧JSON:

    jackson-annotations-2.6.0.jar
    jackson-core-2.6.0.jar
    jackson-databind-2.6.0.jar

  ⑨Hibernate Validate:

    hibernate-validator-4.3.0.Final.jar

    validation-api-1.0.0.GA.jar
    validation-api-1.1.0.CR1.jar

    jboss-logging-3.1.0.CR2.jar
    jboss-logging-3.1.1.GA.jar

3.2、SSM项目的目录结构

    技术分享

3.3、web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5 id="WebApp_ID" version="3.1">
 6 <display-name>SSM_01</display-name>
 7 <welcome-file-list>
 8 <welcome-file>index.jsp</welcome-file>
 9 </welcome-file-list>
10 
11  
12 
13 <!-- 1、配置 Spring 配置文件的名称和位置 -->
14 <context-param>
15 <param-name>contextConfigLocation</param-name>
16 <param-value>classpath:spring/applicationContext.xml</param-value>
17 </context-param>
18 <!-- ContextLoaderListener:监听器 -->
19 <!--当 web项目启动的时候,会把 Spring IOC 容器获取到,并放入 application 域中 -->
20 <listener>
21 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
22 </listener>
23 
24  
25 
26 <!-- 2、配置 SpringMVC 的前端控制器(要过滤所有的请求) -->
27 <servlet>
28 <servlet-name>springDispatcherServlet</servlet-name>
29 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
30 <!-- 配置 SpringMVC 配置文件的名称和位置 -->
31 <init-param>
32 <param-name>contextConfigLocation</param-name>
33 <param-value>classpath:springmvc/springMvc.xml</param-value>
34 </init-param>
35 <load-on-startup>1</load-on-startup>
36 </servlet>
37 
38  
39 
40 <servlet-mapping>
41 <servlet-name>springDispatcherServlet</servlet-name>
42 <!-- SpringMVC 的前端控制器过滤所有的请求 -->
43 <url-pattern>/</url-pattern>
44 </servlet-mapping>
45 
46  
47 
48 <!-- 3、配置 CharacterEncodingFilter:映射出现乱码 -->
49 <filter>
50 <filter-name>encodingFilter</filter-name>
51 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
52 <async-supported>true</async-supported>
53 <init-param>
54 <param-name>encoding</param-name>
55 <param-value>UTF-8</param-value>
56 </init-param>
57 </filter>
58 <filter-mapping>
59 <filter-name>encodingFilter</filter-name>
60 <url-pattern>/*</url-pattern>
61 </filter-mapping>
62 
63  
64 
65 <!-- 4、配置 HiddenHttpMethodFilter:支持 REST 风格的 CRUD -->
66 <filter>
67 <filter-name>HiddenHttpMethodFilter</filter-name>
68 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
69 </filter>
70 
71  
72 
73 <filter-mapping>
74 <filter-name>HiddenHttpMethodFilter</filter-name>
75 <url-pattern>/*</url-pattern>
76 </filter-mapping>
77 </web-app>

3.4、applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4 xmlns:tx="http://www.springframework.org/schema/tx"
 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
 7 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
 8 <!-- 1、配置自动扫描的(Spring IOC 容器不扫描Controller) -->
 9 <context:component-scan base-package="cn.com.zfc.ssm">
10 <context:include-filter type="annotation"
11 expression="org.springframework.stereotype.Component" />
12 <context:include-filter type="annotation"
13 expression="org.springframework.stereotype.Repository" />
14 <context:include-filter type="annotation"
15 expression="org.springframework.stereotype.Service" />
16 </context:component-scan>
17 
18 <!-- 2.加载配数据源配置文件 db.properties -->
19 <context:property-placeholder location="classpath:db.properties" />
20 
21 <!-- 3、配置 C3P0 数据源 -->
22 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
23 <property name="user" value="${user}"></property>
24 <property name="password" value="${password}"></property>
25 <property name="jdbcUrl" value="${jdbcUrl}"></property>
26 <property name="driverClass" value="${driverClass}"></property>
27 </bean>
28 
29 <!-- 4.事务管理器 (JDBC) -->
30 <bean id="transactionManager"
31 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
32 <property name="dataSource" ref="dataSource"></property>
33 </bean>
34 
35 <!-- 5.启动声明式事务驱动 -->
36 <tx:annotation-driven transaction-manager="transactionManager" />
37 
38 <!-- MyBatis 整合 spring -->
39 <!-- 1. spring 通过 sqlSessionFactoryBean 获取 sqlSessionFactory 工厂类 -->
40 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
41 <property name="dataSource" ref="dataSource"></property>
42 <!-- 使用mapper 时可以实用化别名(BeanName) -->
43 <property name="typeAliasesPackage" value="http://www.mamicode.com/cn.com.zfc.ssm.entity"></property>
44 </bean>
45 
46 <!-- 2.打开 sqlSession 会话对象(扫描映射配置文件:XXXmapper.xml/XXXmapper.java) -->
47 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
48 <property name="basePackage" value="http://www.mamicode.com/cn.com.zfc.ssm.dao" />
49 <!-- 注意使用 sqlSessionFactoryBeanName 避免出现spring 扫描组件失效问题 -->
50 <property name="sqlSessionFactoryBeanName" value="http://www.mamicode.com/sqlSessionFactory" />
51 </bean>
52 
53 </beans>

3.5、springMvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
 5 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
 9 
10 
11 <!-- 1、配置自动扫描的包,SpringMVC IOC 容器只扫描Controller -->
12 <context:component-scan base-package="cn.com.zfc.ssm">
13 <context:include-filter type="annotation"
14 expression="org.springframework.stereotype.Controller" />
15 </context:component-scan>
16 
17 <!-- 2、配置视图解析器 -->
18 <bean id="internalResourceViewResolver"
19 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20 <property name="prefix" value="/WEB-INF/views/"></property>
21 <property name="suffix" value=".jsp"></property>
22 </bean>
23 
24 <!-- 3.处理静态资源,默认 Servlet 处理 -->
25 <mvc:default-servlet-handler />
26 
27 <!-- 4.启动 MVC 注解驱动 -->
28 <mvc:annotation-driven />
29 
30 <!--5、启用AOP -->
31 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
32 </beans>

3.6、db.properties

1 user=root
2 password=root
3 driverClass=com.mysql.jdbc.Driver
4 jdbcUrl=jdbc:mysql://localhost:3306/ssm_01

3.7、log4j.properties

 1 # Global logging configuration
 2 log4j.rootLogger=DEBUG, stdout
 3 # Console output...
 4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
 7 
 8 log4j.logger.java.sql.ResultSet=INFO 
 9 log4j.logger.org.apache=INFO 
10 log4j.logger.java.sql.Connection=DEBUG 
11 log4j.logger.java.sql.Statement=DEBUG 
12 log4j.logger.java.sql.PreparedStatement=DEBUG

3.8、实体类:User.java

 1 package cn.com.zfc.ssm.entity;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 /**
 6 * 定义实体类:如果实体类的属性名和数据表中的字段名不一致,则需要在 映射文件中配置 resultMap
 7 * 
 8 * @author FuchangZhang
 9 *
10 */
11 @Component
12 public class User {
13   private Integer id;
14   private String userName;
15   private String userSex;
16   private Integer userAge;
17 
18   public User() {
19     super();
20   }
21 
22   public User(String userName, String userSex, Integer userAge) {
23     super();
24     this.userName = userName;
25     this.userSex = userSex;
26     this.userAge = userAge;
27   }
28 
29   public User(Integer id, String userName, String userSex, Integer userAge) {
30     super();
31     this.id = id;
32     this.userName = userName;
33     this.userSex = userSex;
34     this.userAge = userAge;
35   }
36 
37   public Integer getId() {
38     return id;
39   }
40 
41   public void setId(Integer id) {
42     this.id = id;
43   }
44 
45   public String getUserName() {
46     return userName;
47   }
48 
49   public void setUserName(String userName) {
50     this.userName = userName;
51   }
52 
53   public String getUserSex() {
54     return userSex;
55   }
56 
57   public void setUserSex(String userSex) {
58     this.userSex = userSex;
59   }
60 
61   public Integer getUserAge() {
62     return userAge;
63   }
64 
65   public void setUserAge(Integer userAge) {
66     this.userAge = userAge;
67   }
68 
69   @Override
70   public String toString() {
71     return "User [id=" + id + ", userName=" + userName + ", userSex=" + userSex + ", userAge=" + userAge + "]";
72   }
73 
74 }

3.9 UserMapper.java

 1 package cn.com.zfc.ssm.dao;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.stereotype.Repository;
 6 
 7 import cn.com.zfc.ssm.entity.User;
 8 
 9 /**
10 * 定义接口:方法名得和相应的 mapper.xml 中的 id 保持一致
11 * 
12 * @author FuchangZhang
13 *
14 */
15 @Repository
16 public interface UserMapper {
17 
18   // 添加一条用户信息
19   public int save(User user);
20 
21   // 删除一条用户信息
22   public int delete(int id);
23 
24   // 修改一条用户信息
25   public int update(User user);
26 
27   // 查询一条用户信息
28   public User getUser(int id);
29 
30   // 查询全部用户信息
31   public List<User> getUsers();
32 
33 }

3.10 UserMapper.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="cn.com.zfc.ssm.dao.UserMapper">
 4 <!-- 结果集映射:映射实体类属性与数据表字段名 -->
 5 <resultMap type="User" id="userResultMap">
 6 <id property="id" column="id" />
 7 <result property="userName" column="user_name" />
 8 <result property="userSex" column="user_sex" />
 9 <result property="userAge" column="user_age" />
10 </resultMap>
11 
12 <!-- 参数映射:映射实体类属性与数据表字段名 -->
13 <!-- resultMap:resultMap的id ,property:resultMap的property,即实体类中的属性 -->
14 <parameterMap type="User" id="userParameterMap">
15 <parameter property="id" resultMap="userResultMap" />
16 <parameter property="userName" resultMap="userResultMap" />
17 <parameter property="userSex" resultMap="userResultMap" />
18 <parameter property="userAge" resultMap="userResultMap" />
19 </parameterMap>
20 
21 <!-- 添加一个用户信息 -->
22 <insert id="save" parameterMap="userParameterMap">
23 insert into user_info
24 (user_name,user_sex,user_age)
25 values
26 (#{userName},#{userSex},#{userAge})
27 </insert>
28 <!-- 删除一个用户信息 -->
29 <delete id="delete" parameterType="int">
30 delete from user_info
31 where
32 id=#{id}
33 </delete>
34 
35 <!-- 修改一个用户信息 -->
36 <update id="update" parameterMap="userParameterMap">
37 update user_info
38 set
39 user_name=#{userName},user_sex=#{userSex},user_age=#{userAge}
40 where
41 id=#{id}
42 </update>
43 
44 <!-- 查询一个用户信息 -->
45 <select id="getUser" parameterType="int" resultMap="userResultMap">
46   select * from
47   user_info
48   where
49   id=#{id}
50 </select>
51 
52 <!-- 查询全部用户信息 -->
53 <select id="getUsers" resultMap="userResultMap">
54 select * from user_info
55 </select>
56 </mapper>

3.11、UserService.java

 1 package cn.com.zfc.ssm.service;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.stereotype.Repository;
 6 
 7 import cn.com.zfc.ssm.entity.User;
 8 
 9 @Repository
10 public interface UserService {
11   // 添加一条用户信息
12   public boolean add(User user);
13 
14   // 删除一条用户信息
15   public boolean delete(int id);
16 
17   // 修改一条用户信息
18   public boolean update(User user);
19 
20   // 查询一条用户信息
21   public User queryOne(int id);
22 
23   // 查询全部用户信息
24   public List<User> queryAll();
25 }

3.12、UserServiceImpl.java

 1 package cn.com.zfc.ssm.service.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 import cn.com.zfc.ssm.dao.UserMapper;
 9 import cn.com.zfc.ssm.entity.User;
10 import cn.com.zfc.ssm.service.UserService;
11 
12 @Service("userService")
13 public class UserServiceImpl implements UserService {
14 
15   @Autowired
16   private UserMapper userMapper;
17   private int result;
18 
19   @Override
20   public boolean add(User user) {
21     result = userMapper.save(user);
22     if (result > 0) {
23       return true;
24     } else {
25       return false;
26     }
27   }
28 
29   @Override
30   public boolean delete(int id) {
31     result = userMapper.delete(id);
32     if (result > 0) {
33       return true;
34     } else {
35       return false;
36     }
37   }
38 
39   @Override
40   public boolean update(User user) {
41     result = userMapper.update(user);
42     if (result > 0) {
43       return true;
44     } else {
45       return false;
46     }
47   }
48 
49   @Override
50   public User queryOne(int id) {
51     User user = userMapper.getUser(id);
52     return user;
53   }
54 
55   @Override
56   public List<User> queryAll() {
57     List<User> users = userMapper.getUsers();
58     return users;
59   }
60 
61 }

3.13、UserHandler.java

 1 package cn.com.zfc.ssm.handler;
 2 
 3 import java.util.Map;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 
 9 import cn.com.zfc.ssm.entity.User;
10 import cn.com.zfc.ssm.service.UserService;
11 
12 @Controller
13 public class UserHandler {
14 
15 @Autowired
16 private UserService userService;
17 
18   // 添加页面
19   @RequestMapping("/addView")
20   public String addView(Map<String, Object> map) {
21     map.put("user", new User());
22     return "addUser";
23   }
24 
25   // 保存操作
26   @RequestMapping("/addUser")
27   public String addUser(User user) {
28     if (userService.add(user)) {
29       return "success";
30     } else {
31       return "redirect:/queryAll";
32     }
33   }
34 
35   // 删除操作
36   @RequestMapping("/delete")
37   public String delete(int id) {
38     if (userService.delete(id)) {
39       System.out.println("删除成功");
40     } else {
41       System.out.println("删除失败");
42     }
43     return "redirect:/queryAll";
44   }
45 
46   // 查询一个操作
47   @RequestMapping("/queryOne")
48   public String queryOne(int id, Map<String, Object> map) {
49     map.put("user", userService.queryOne(id));
50     return "update";
51   }
52 
53   // 修改操作
54   @RequestMapping("/update")
55   public String update(User user, Map<String, Object> map) {
56     if (userService.update(user)) {
57       System.out.println("修改成功");
58       return "redirect:/queryAll";
59     } else {
60       System.out.println("修改失败");
61       map.put("user", userService.queryOne(user.getId()));
62       return "update";
63     }
64   }
65 
66   // 查询全部操作
67   @RequestMapping("/queryAll")
68   public String queryAll(Map<String, Object> map) {
69     map.put("users", userService.queryAll());
70     return "queryAll";
71   }
72 }

3.14、index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2 pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <a href="${pageContext.request.contextPath}/addView">添加用户</a>
11 <hr />
12 <a href="${pageContext.request.contextPath}/queryAll">查询全部</a>
13 </body>
14 </html>

3.15、addUser.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2 pageEncoding="UTF-8"%>
 3 <%@ page import="java.util.*"%>
 4 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>用户添加</title>
11 </head>
12 <body>
13 <h1>用户添加</h1>
14 <form:form action="addUser" method="post" modelAttribute="user">
15 
16 UserName:<form:input path="userName" />
17 <hr />
18 <%
19 Map<String, Object> sexes = new HashMap<>();
20 sexes.put("", "");
21 sexes.put("", "");
22 request.setAttribute("sexes", sexes);
23 %>
24 Sex:
25 <form:radiobuttons path="userSex" items="${sexes}" />
26 <hr />
27 UserAge:<form:input path="userAge" />
28 <hr />
29 <input type="submit" value="添加" />
30 </form:form>
31 </body>
32 </html>

3.16、success.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2 pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <h1>Success Page!!!</h1>
11 </body>
12 </html>

3.17、queryAll.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2 pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 <style type="text/css">
11 table, tr, th, td {
12 border: 1px solid black;
13 }
14 
15 table {
16 width: 80%;
17 }
18 </style>
19 </head>
20 <body>
21 <table>
22 <thead>
23 <tr>
24 <th>ID</th>
25 <th>UserName</th>
26 <th>UserSex</th>
27 <th>UserAge</th>
28 <th>Operator</th>
29 </tr>
30 </thead>
31 <tbody>
32 <c:choose>
33 <c:when test="${requestScope.users.size()>0 }">
34 <c:forEach items="${requestScope.users}" var="user">
35 <tr>
36 <td>${user.id }</td>
37 <td>${user.userName }</td>
38 <td>${user.userSex }</td>
39 <td>${user.userAge }</td>
40 <td><a
41 href="http://www.mamicode.com/${pageContext.request.contextPath}/delete?id=${user.id}">Delete</a>
42 <a
43 href="http://www.mamicode.com/${pageContext.request.contextPath}/queryOne?id=${user.id}">Update</a>
44 </td>
45 </tr>
46 </c:forEach>
47 </c:when>
48 <c:otherwise>没有人</c:otherwise>
49 </c:choose>
50 </tbody>
51 </table>
52 
53 </body>
54 </html>

3.18、update.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2 pageEncoding="UTF-8"%>
 3 <%@ page import="java.util.*"%>
 4 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>Insert title here</title>
11 </head>
12 <body>
13 <h1>用户修改</h1>
14 <form:form action="${pageContext.request.contextPath}/update"
15 method="post" modelAttribute="user">
16 <!-- 隐藏域:用来存储要修改的 user 的 id -->
17 <input type="hidden" name="id" value="${user.id}" />
18 UserName:<form:input path="userName" />
19 <hr />
20 <%
21 Map<String, Object> sexes = new HashMap<>();
22 sexes.put("", "");
23 sexes.put("", "");
24 request.setAttribute("sexes", sexes);
25 %>
26 Sex:
27 <form:radiobuttons path="userSex" items="${sexes}" />
28 <hr />
29 UserAge:<form:input path="userAge" />
30 <hr />
31 <input type="submit" value="http://www.mamicode.com/修改" />
32 </form:form>
33 </body>
34 </html>

SSM项目整合