项目目录结构
创建mavenWeb项目
pom文件
spring依赖、spring-web依赖、spring-webmvc依赖、spring-test、junit依赖
servlet依赖、jsp依赖、jstl、standard依赖
jackson的core和databind和annotations依赖、fastjson依赖、
文件上传的commons-fileupload和commons-io依赖
日志commons-logging、slf4j-log4j12、log4j依赖
mysql依赖、c3p0依赖、druid依赖、spring-jdbc依赖、spring-tx依赖
lombok依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringDemoModule</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.74</version>
</dependency>
<dependency><!--文件上传-->
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency><!--文件上传-->
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency><!--tx事务-->
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/SpringDemoModule</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
配置web容器启动时创建spring上下文对象到servletContext
配置springMvc的post请求的中文乱码的编码过滤器
配置springMvc的前端控制器DispatcherServlet
<?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><!--servletContext.getInitParameter("contextConfigLocation)、jsp中通过el表达式-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置Spring的监听器:spring集成web里的
1.web.xml中配置全局初始化参数contextConfigLocation和org.springframework.web.context.ContextLoaderListener的listener
当web项目启动加载初始化参数contextConfigLocation,并根据classpath:applicationContext.xml进行new出ApplicationContext对象app,
将app对象通过setAttribute("app",app)放入当前web应用最大的域servletContext域中域中,并需要时getAttribute取出该app对象。
2.还有一种方式为定义监听类和@WebListener注解方式详见spring的博客中集成web部分。-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springMvc字符编码过滤器,解决post请求中文乱码问题-->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置SpringMVC的前端控制器:
配置一个servlet,类选择第三方org.springframework的DispatcherServlet
配置该类,且路径为拦截所有来作为springMvc的前端控制器servlet -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param><!--该servlet的初始化参数-->
<!--spring-mvc.xml是怎么加载的呢
spring-mvc.xml是怎么加载的呢
类似web.xml中的<context-param>application域的全局初始化参数。
这里下面配置初始化参数spring-mvc.xml名称即为web.xml中该DispatcherServlet的servlet的init初始化参数来提供,
以便org的DispatcherServlet根据这个servlet的init初始化参数去加载springMVC的配置文件,
继而IOC出controller(特有行为)实例提供给前端控制器DispatcherServlet(共有行为)使用。
-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup><!--loadOnStartup=负整数或不加默认第一次访问该servlet执行时创建servlet对象并初始化
loadOnStartup为0或正整数时,web服务器启动时创建servlet对象,数字越小,优先级越高-->
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern><!-- 配置该servlet的路径为拦截所有/,虽会覆盖tomcat静态资源访问路径,但现在没有静态资源html之类,只有jsp动态页面。-->
</servlet-mapping>
</web-app>
resource里log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
resource下jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jdbc
jdbc.username=root
jdbc.password=root123
resource下applicationContext.xml
配置组件扫描、引入jdbc.properties、配置并接管c3p0数据源、配置并接管jdbcTemplate
<?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"
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">
<context:component-scan base-package="com.kdy"/><!--开启组件扫描-->
<context:property-placeholder location="classpath:jdbc.properties"/><!--引入配置文件,使用el获取-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
com.kdy.converter包中创建DateConverter
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String dateStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
resource下spring-mvc.xml
配置controller的组件扫描、配置并接管视图解析器、配置上并接管日期转换器、配置并接管文件上传解析器、配置注解驱动(可对象转json)并配置日期转换器属性、配静态资源放行。
<?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: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
">
<!--Controller的组件扫描-->
<context:component-scan base-package="com.kdy"></context:component-scan>
<!--配置内部资源视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property><!--前缀-->
<property name="suffix" value=".jsp"></property><!--后缀-->
</bean>
<!--声明springMvc请求参数为date=2021-02-22时controller入参为Date date的类型转换器
1.定义这个bean
2.在springMvc注解驱动中传递这个转换器的bean的id <mvc:annotation-driven conversion-service="conversionService"/> -->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.kdy.converter.DateConverter"></bean>
</list>
</property>
</bean>
<!--配置文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--上传文件的编码类型-->
<property name="defaultEncoding" value="UTF-8"/>
<!--上传文件总大小,最大限制,单位byte-->
<property name="maxUploadSize" value="5242800"/>
<!--上传单个文件大小,最大限制,单位byte-->
<property name="maxUploadSizePerFile" value="5242800"/>
</bean>
<!--配置处理器映射器-->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>-->
<!--注解驱动,顶替配置的上面配置的处理器映射器和适配器,集成了jackson,可Controller资源方法返回字为对象User时,自动转为json-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!--js和css和html等被web.xml中配置的前端控制配置拦截了,
这里配置springMvc静态资源放行-->
<!--springMvc开放静态资源的访问路径。mapping表示的是映射地址,location表示的是哪个目录下的资源是对外开放的-->
<mvc:resources mapping="/js/**" location="/js/"/><!--就表示访问路径为/js/**时,在/js/目录下的资源是对外开放的-->
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/html/**" location="/html/"/>
<!--上面的<mvc:resources location="/" mapping="/**/*.js"/>等等也可配置为以下代码-->
<!-- <mvc:default-servlet-handler/>若使用这种方式,必须配置注解驱动,否则controller无法访问-->
<!-- 意思就是在访问资源时,springMvc帮我们找对于controller的地址,
如果找不到,就交给原始容器tomcat容器帮我们找静态资源的地址。-->
</beans>
数据表
com.kdy.domain下
@Data//包括get、set、equals、hashCode、canEqual、toString、无参构造,没有有参构造
@NoArgsConstructor
@AllArgsConstructor
public class Role {
private Long id;
private String roleName;
private String roleDesc;
}
@Data//包括get、set、equals、hashCode、canEqual、toString、无参构造,没有有参构造
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
private String password;
private String email;
private Date birthday;
private Integer infoId;
private String englishTeacher;
//当前用户具备的角色
private List<Role> roles;
}
com.kdy.dao下
public interface RoleDao {
List<Role> selectAll();
void save(String roleName,String roleDesc);
List<Role> selectRolesByUid(Integer uid);
}
public interface UserDao {
List<User> selectAll();
Long save(User user);
void saveUserRoleRelation(Integer uid,Integer [] roleIds);
void delUserRoleRel(Integer uid);
void delUser(Integer uid);
}
com.kdy.dao的impl下
@Repository
public class RoleDaoImpl implements RoleDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Role> selectAll() {
return jdbcTemplate.query("select * from sys_role", new BeanPropertyRowMapper<Role>(Role.class));
}
public void save(String roleName,String roleDesc) {
jdbcTemplate.update("insert into sys_role(role_name,role_desc) values(?,?)", roleName, roleDesc);
}
@Override
public List<Role> selectRolesByUid(Integer uid) {
return jdbcTemplate.query("select * from sys_role where id in(select rid from sys_user_role where uid = ? )", new BeanPropertyRowMapper<Role>(Role.class),uid);
}
}
注:jdbcTemplate新增操作的主键返回
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<User> selectAll() {
return jdbcTemplate.query("select * from sys_user", new BeanPropertyRowMapper<User>(User.class));
}
/*
* 主键返回
* */
@Override
public Long save(User user) {
//想要新增操作返回主键,不能使用下面这个新增操作的api
//jdbcTemplate.update("insert into sys_user(name,password,email,birthday,infoId,english_teacher) values(?,?,?,?,?,?)", user.getName(), user.getPassword(), user.getEmail(), user.getBirthday(), user.getInfoId(), user.getEnglishTeacher());、
String sql = "insert into sys_user(name,password,email,birthday,infoId,english_teacher) values(?,?,?,?,?,?)";
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, user.getName());
ps.setString(2, user.getPassword());
ps.setString(3, user.getEmail());
ps.setDate(4, new java.sql.Date(user.getBirthday().getTime()));
ps.setInt(5, user.getInfoId());
ps.setString(6, user.getEnglishTeacher());
return ps;
}
}, keyHolder);
Long generatedId = keyHolder.getKey().longValue();
return generatedId;
}
@Override
public void saveUserRoleRelation(Integer uid, Integer[] roleIds) {
for (Integer rid:roleIds) {
jdbcTemplate.update("insert into sys_user_role(uid,rid) values(?,?)", uid, rid);
}
}
@Override
public void delUserRoleRel(Integer uid) {
jdbcTemplate.update("delete from sys_user_role where uid = ?",uid);
}
@Override
public void delUser(Integer uid) {
jdbcTemplate.update("delete from sys_user where id = ?",uid);
}
}
com.kdy.service下
public interface RoleService {
List<Role> list();
void save(String roleName,String roleDesc);
}
public interface UserService {
List<User> list();
List<Role> selectRolesByUid(Integer uid);
void save(User user,Integer [] roleIds);
void deleteUser(Integer userId);
}
com.kdy.service的impl下
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleDao roleDao;
@Override
public List<Role> list() {
return roleDao.selectAll();
}
@Override
public void save(String roleName,String roleDesc) {
roleDao.save(roleName,roleDesc);
}
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Autowired
private RoleDao roleDao;
@Override
public List<User> list() {
return userDao.selectAll();
}
@Override
public List<Role> selectRolesByUid(Integer uid) {
return roleDao.selectRolesByUid(uid);
}
@Override
public void save(User user,Integer [] roleIds) {
Long uid = userDao.save(user);//经过主键返回处理了,返回的是新增数据的数据库生成的主键
userDao.saveUserRoleRelation(uid.intValue(),roleIds);
}
@Override
public void deleteUser(Integer userId) {
userDao.delUserRoleRel(userId);
userDao.delUser(userId);
}
}
com.kdy.controller
@Controller
@RequestMapping("/role")
public class RoleController {
@Autowired
private RoleService roleService;
@RequestMapping("/roleList")
public ModelAndView list() {
ModelAndView modelAndView = new ModelAndView();
List<Role> roleList = roleService.list();
modelAndView.addObject("roleList",roleList);//放到request域中
modelAndView.setViewName("role-list");//这里字符串会经过xml中配置的视图解析器处理,拼上前缀后缀,请求转发
return modelAndView;
}
@RequestMapping("/save")
public String save(HttpServletRequest req,String roleName,String roleDesc) {
try{
roleService.save(roleName,roleDesc);
return "redirect:/role/roleList";
}catch (Exception e){
e.printStackTrace();
req.setAttribute("error_msg","保存失败");
return "role-add";
}
}
}
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
@RequestMapping("/userList")
public ModelAndView list() {
ModelAndView modelAndView = new ModelAndView();
List<User> userList = userService.list();
for (User user : userList) {
List<Role> roles = userService.selectRolesByUid(user.getId());
user.setRoles(roles);
}
modelAndView.addObject("userList", userList);//放到request域中
modelAndView.setViewName("user-list");//这里字符串会经过xml中配置的视图解析器处理,拼上前缀后缀,请求转发
return modelAndView;
}
@RequestMapping("/saveUI")
public String saveUI(HttpServletRequest request) {//用这种request加返回String返回页面的方式和上面的ModelAndView的方式均可,不过ModelAndView的方式在jsp页面有代码提示
List<Role> roleList = roleService.list();
request.setAttribute("roleList", roleList);
return "user-add";
}
@RequestMapping("/save")
public String save(User user, Integer[] roleIds, HttpServletRequest req) {//这里使用User对象封装参数,一个个的写入参也行。
try {
userService.save(user, roleIds);
return "redirect:/user/userList";
} catch (Exception e) {
e.printStackTrace();
req.setAttribute("error_msg", "保存失败");
return "user-add";
}
}
@RequestMapping("/del/{userId}")/*由于客户端使用的restFul风格传参,所以controller接收restful风格参水*/
public String del(@PathVariable("userId") Integer uid) {
userService.deleteUser(uid);
return "redirect:/user/userList";
}
}
webapp的jsp下
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/jsp/index.jsp">首页</a><br/>
<a href="${pageContext.request.contextPath}/user/userList">用户管理</a><br/>
<a href="${pageContext.request.contextPath}/role/roleList">角色管理</a><br/>
</body>
</html>
role-list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!--引入jstl标签库URI-->
<html>
<head>
<title>RoleList</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/jsp/role-add.jsp">新增</a><br/>
<a href="${pageContext.request.contextPath}/role/roleList">刷新</a><br/>
<table border="1px">
<thead>
<tr>
<td>选中</td>
<td>id</td>
<td>角色名</td>
<td>角色描述</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<c:forEach items="${roleList}" var="role">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${role.id}</td>
<td>${role.roleName}</td>
<td>${role.roleDesc}</td>
<td><a href="#">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
role-add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>role-add</title>
</head>
<body>
<p>${error_msg}</p>
<form action="${pageContext.request.contextPath}/role/save" method="post">
角色名称:<input name="roleName" type="text"><br/>
角色描述:<input name="roleDesc" type="text"><br/>
<input type="submit" value="提交"><br/>
<input type="reset" value="重置"><br/>
</form>
</body>
</html>
user-list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!--引入jstl标签库URI-->
<html>
<head>
<title>UserList</title>
<script>
function delUser(userId) {
if(confirm("你要删除吗?")){
location.href="${pageContext.request.contextPath}/user/del/"+userId;/*这里使用Restful风格传参*/
}
}
</script>
</head>
<body>
<a href="${pageContext.request.contextPath}/user/saveUI">新增</a><br/>
<a href="${pageContext.request.contextPath}/user/userList">刷新</a><br/>
<table border="1px">
<thead>
<tr>
<td>选中</td>
<td>id</td>
<td>用户名</td>
<td>邮箱</td>
<td>生日</td>
<td>infoId</td>
<td>英语老师</td>
<td>具备角色</td>
</tr>
</thead>
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.email}</td>
<td>${user.birthday}</td>
<td>${user.infoId}</td>
<td>${user.englishTeacher}</td>
<td>
<c:forEach items="${user.roles}" var="role">
${role.roleName}
</c:forEach>
</td>
<td><a href="javascript:void(0);" onclick="delUser(${user.id})">删除</a></td>
<%--javascript:void(0)让该超链接不跳转到任何地方,而鼠标移上去有小手--%>
<%--onclick点击时执行的方法--%>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
user-add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!--引入jstl标签库URI-->
<html>
<head>
<title>user-add</title>
</head>
<body>
<p>${error_msg}</p>
<form action="${pageContext.request.contextPath}/user/save" method="post">
角色名称:<input name="name" type="text"><br/>
密码:<input name="password" type="text"><br/>
邮箱:<input name="email" type="text"><br/>
生日:<input name="birthday" type="date"><br/>
infoId:<input name="infoId" type="text"><br/>
英语老师:<input name="englishTeacher" type="text"><br/>
角色:
<c:forEach items="${roleList}" var="role">
<input type="checkbox" name="roleIds" value="${role.id}">${role.roleName}
</c:forEach><br/>
<input type="submit" value="提交"><br/>
<input type="reset" value="重置"><br/>
</form>
</body>
</html>