今天是实习第六天,主要的任务就是增加一个过滤器功能,防止用户没有登录就可以看到数据,然后再增加一个功能:给用户增加角色
创建表格
增加角色就是可以给用户增加管理员身份或者普通用户身份这两种,首先我们还需要新建两张表
t_role表存放用户角色以下信息:
user_role表存放:
其中,userId和roleId是两个外键,分别对应t_user表的id和t_role表的id,下面介绍如何创建外键。
我是使用的navicate创建的外键,给user_role表创建两个外键,直接放截图如下
名:不用填写,系统会自动填写
栏位:外键的名称
参考表:该外键是哪张表的外键,即跟哪张表关联(我是这样理解的)
参考栏位:外键与参考表对应的关联字段
删除时:选择CASCADE,那么该表更新时,会同时更新
更新时:同删除时
一开始一直创建失败,最好遇到问题时多在网上找找解决方法,就会找到解决方法。我创建外键失败的有一个原因是:userId和id不是同一个类型,最后修改后创建成功。
==============================================
过滤器:
首先修改web.xml
<!-- 7.26 -->
<!-- 过滤器 -->
<filter>
<filter-name>loginfilter</filter-name>
<filter-class>com.zhongruan.filter.LoginFilter</filter-class>
</filter>
<!-- 过滤器的映射 -->
<filter-mapping>
<filter-name>loginfilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
创建一个LoginFilter类
内容如下:
package com.zhongruan.filter;
import com.zhongruan.bean.User;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤器执行====");
// 1. 得到HttpServletRequest
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
// 2. 得到session
HttpSession session = request.getSession();
// 3. 得到User对象
User user = (User) session.getAttribute("user");
// 4. 得到请求路径
String url = request.getRequestURI();
// 5. 判断session是否存在user对象 如果存在 表示已登录 不存在 则未登录 indexOf
// indexOf 方法的作用 如果包含子字符串 则返回 子字符串的索引 如果不包含 则返回 -1
if (user == null && url.indexOf("login.do") == -1) {
System.out.println("登录信息不存在, 并且访问登录接口以外的其他接口,应进行拦截");
response.sendRedirect(request.getContextPath() + "/pages/login.jsp");
} else {
// 过滤器的放行
System.out.println("登录信息已存在");
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Override
public void destroy() {
System.out.println("过滤器销毁");
}
}
利用快捷键,将所有的findAll03替换成findAll03.do
再将userHandler里面的 形如/toUpdate替换成:
修改后的userHandler(里面包含了增加角色功能的实现方法)
package com.zhongruan.controller;
import com.zhongruan.bean.PageInfo;
import com.zhongruan.bean.Role;
import com.zhongruan.bean.User;
import com.zhongruan.dao.UserDao;
import com.zhongruan.service.RoleService;
import com.zhongruan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserHandler {
// @Controller 表示UserHandler类 是一个控制器类(处理请求)
@Autowired
UserDao userDao;
@Autowired
UserService userService;
@Autowired
RoleService roleService;
@RequestMapping("/findAll")
public ModelAndView index() {
System.out.println("index方法执行 ");
ModelAndView mv = new ModelAndView();
mv.addObject("list", userService.findAll());
mv.setViewName("list");
return mv;
// ModelAndView 数据视图
// return "index";
}
@RequestMapping("/findAll01")
public ModelAndView index01() {
ModelAndView mv = new ModelAndView();
mv.addObject("list", userService.findAll());
mv.setViewName("list");
return mv;
}
/* 7.26更新
@RequestMapping("/login")
public String login(String username, String password) {
System.out.println(username + "====" + password);
// 对 用户名和密码 进行 校验
User user = userService.login(new User(username, password));
System.out.println(user);
if (user == null) {
System.out.println("登录失败");
return "pages/failer";
} else {
System.out.println("登录成功");
return "pages/success";
}
}
*/
/*7.26更新login方法*/
// 7.24 新增session功能
@RequestMapping("/login.do")
public String login(String username, String password, HttpSession session) {
System.out.println(username + "====" + password);
// 对 用户名和密码 进行 校验
User user = userService.login(new User(username, password));
System.out.println(user);
if (user == null) {
System.out.println("登录失败");
return "pages/failer";
} else {
// 将user对象 存储到 session中
session.setAttribute("user", user);
// 7.26 查询用户角色
List<Integer> list = roleService.findRoleId(user.getId());
session.setAttribute("roleIds", list);
System.out.println("登录成功");
return "pages/success";
}
}
@RequestMapping("/register")
public String register(String username, String password, String check_password) {
if (password.equals(check_password)) {
System.out.println(username + "====" + password);
userService.register(new User(username, password));
System.out.println("注册成功");
return "pages/success";
}else {
System.out.println("两次密码输入不一致");
// 在这里把对应的值传到session里
return "pages/failer";
}
}
@RequestMapping("/findAll02")
public ModelAndView findAll(){
ModelAndView mv = new ModelAndView();
mv.setViewName("pages/dataList");
mv.addObject("list", userService.findAll());
return mv;
}
@RequestMapping("/delete.do")
public String delete(int id){
userService.deleteByID(id);
return "redirect:findAll03.do";
}
//将数据展示在界面上 更新的前置操作
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id){
ModelAndView mv = new ModelAndView();
User user = userService.findByID(id);
mv.addObject("user", user);
mv.setViewName("pages/user-update");
return mv;
}
@RequestMapping("/update.do")
public String Update(User user){
int flag = userService.update(user);
if(flag==1){
System.out.println("修改成功!");
}else{
System.out.println("修改失败!");
}
return "redirect:findAll03.do";
}
/* 7.24更新注释掉7.23的方法
//分页 currentPage当前页数
@RequestMapping("/findAll03.do")
public ModelAndView findAllByPage(@RequestParam(defaultValue = "1") int currentPage) {
ModelAndView mv = new ModelAndView();
PageInfo<User> pageInfo = userService.findByPage(currentPage);
System.out.println(pageInfo);
// 将数据返回到页面上
mv.setViewName("pages/dataList");
mv.addObject("pageInfo", pageInfo);
return mv;
}
*/
/* 7.24更新 批量删除 */
@RequestMapping("/deleteAll.do")
public String deleteAll(String userList) {
// System.out.println(userList); // 2,3,8
// split 字符串切割
String[] strs = userList.split(",");
List<Integer> ids = new ArrayList<>();
// for-in 循环
for(String s : strs) {
ids.add(Integer.parseInt(s));
}
userService.deleteAll(ids);
return "redirect:findAll03.do";
}
@RequestMapping("/findAll03.do")
public ModelAndView findAllByPage(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "0") int type, String username, HttpSession session) {
// 判断 是否是 条件查询
if (type == 1) {
session.setAttribute("username", username);
} else {
System.out.println("========");
session.setAttribute("username", null);
// username = (String) session.getAttribute("username");
}
ModelAndView mv = new ModelAndView();
PageInfo<User> pageInfo = userService.findByPage(currentPage, username);
System.out.println(pageInfo);
//指定页面dataList
mv.setViewName("pages/dataList");
mv.addObject("pageInfo", pageInfo);
return mv;
}
//7.26更新
@RequestMapping("toAddRole")
public ModelAndView toAddRole(int id) {
List<Role> roleList = roleService.findRoleByUserId(id);
ModelAndView mv = new ModelAndView();
mv.addObject("roles", roleList);
mv.addObject("id", id);
mv.setViewName("/pages/user-role-add");
return mv;
}
@RequestMapping("addRole.do")
@ResponseBody
public String add(String roleList, String userId) {
System.out.println(roleList);
System.out.println(userId);
String[] strs = roleList.split(",");
List<Integer> ids = new ArrayList<>();
for (String str : strs) {
ids.add(Integer.parseInt(str));
}
roleService.add(ids, userId);
return "";
}
}
===========================================
增加角色功能:
界面新增:
以上为需要新增的内容
具体实现:
Role类和UserRole类就省略不写了,参考user类和menu类
Rule_Dao
package com.zhongruan.dao;
import com.zhongruan.bean.Role;
import com.zhongruan.bean.UserRole;
import java.util.List;
public interface RoleDao {
List<Integer> findRoleByUserId(int userId);
List<Role> findRoleInfo(int id);
void addRole(UserRole userRole);
}
RuleDao.xml
<?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.zhongruan.dao.RoleDao">
<select id="findRoleByUserId" parameterType="int" resultType="int">
select roleId from user_role where userId = #{userId}
</select>
<select id="findRoleInfo" parameterType="int" resultType="Role">
SELECT * FROM t_role WHERE id NOT IN (SELECT roleId FROM user_role WHERE userId = #{id})
</select>
<insert id="addRole" parameterType="UserRole">
insert into user_role(userId, roleId) values (#{userId}, #{roleId})
</insert>
</mapper>
RoleService
package com.zhongruan.service;
import com.zhongruan.bean.Role;
import java.util.List;
public interface RoleService {
List<Integer> findRoleId(int userId);
List<Role> findRoleByUserId(int userId);
void add(List<Integer> ids, String userId);
}
RoleServiceImpl
package com.zhongruan.service.impl;
import com.zhongruan.bean.Role;
import com.zhongruan.bean.UserRole;
import com.zhongruan.dao.RoleDao;
import com.zhongruan.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
RoleDao roleDao;
@Override
public List<Integer> findRoleId(int userId) {
return roleDao.findRoleByUserId(userId);
}
@Override
public List<Role> findRoleByUserId(int userId) {
return roleDao.findRoleInfo(userId);
}
@Override
public void add(List<Integer> ids, String userId) {
for(int id : ids) {
UserRole userRole = new UserRole();
userRole.setUserId(Integer.parseInt(userId));
userRole.setRoleId(id);
roleDao.addRole(userRole);
}
}
}
user-role-add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>数据 - AdminLTE2定制版</title>
<meta name="description" content="AdminLTE2定制版">
<meta name="keywords" content="AdminLTE2定制版">
<!-- Tell the browser to be responsive to screen width -->
<meta
content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"
name="viewport">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/ionicons/css/ionicons.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/iCheck/square/blue.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/morris/morris.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/jvectormap/jquery-jvectormap-1.2.2.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/datepicker/datepicker3.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/daterangepicker/daterangepicker.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/datatables/dataTables.bootstrap.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.theme.default.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/select2/select2.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/colorpicker/bootstrap-colorpicker.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/bootstrap-markdown/css/bootstrap-markdown.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/adminLTE/css/AdminLTE.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/adminLTE/css/skins/_all-skins.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/css/style.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.skinNice.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/bootstrap-slider/slider.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.css">
</head>
<body class="hold-transition skin-purple sidebar-mini">
<div class="wrapper">
<!-- 页面头部 -->
<jsp:include page="header.jsp"></jsp:include>
<!-- 页面头部 /-->
<!-- 导航侧栏 -->
<jsp:include page="aside.jsp"></jsp:include>
<!-- 导航侧栏 /-->
<!-- 内容区域 -->
<div class="content-wrapper">
<!-- 内容头部 -->
<section class="content-header">
<h1>
用户管理 <small>添加角色表单</small>
</h1>
<ol class="breadcrumb">
<li><a href="${pageContext.request.contextPath}/index.jsp"><i
class="fa fa-dashboard"></i> 首页</a></li>
<li><a
href="#">用户管理</a></li>
<li class="active">添加角色表单</li>
</ol>
</section>
<!-- 内容头部 /-->
<form
action=""
method="post">
<!-- 正文区域 -->
<section class="content">
<%-- 隐藏域 --%>
<input type="hidden" name="userId" value="${id}">
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right: 0px">
<input id="selall"
type="checkbox" class="icheckbox_square-blue"></th>
<th class="sorting_asc">ID</th>
<th class="sorting">角色名称</th>
<th class="sorting">角色描述</th>
</tr>
</thead>
<tbody>
<c:forEach items="${roles}" var="role">
<tr>
<td>
<input name="roleId" type="checkbox" value="${role.id}">
</td>
<td>${role.id}</td>
<td>${role.rolename }</td>
<td>${role.roledesc}</td>
</tr>
</c:forEach>
</tbody>
</table>
<!--订单信息/--> <!--工具栏-->
<div class="box-tools text-center">
<button type="button" class="btn bg-maroon" onclick="addRoles()">保存</button>
<button type="button" class="btn bg-default"
onclick="history.back(-1);">返回</button>
</div>
<!--工具栏/--> </section>
<!-- 正文区域 /-->
</form>
</div>
<!-- 内容区域 /-->
</div>
<script
src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/jQueryUI/jquery-ui.min.js"></script>
<script>
$.widget.bridge('uibutton', $.ui.button);
</script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/raphael/raphael-min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/morris/morris.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/sparkline/jquery.sparkline.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/knob/jquery.knob.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/daterangepicker/moment.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/daterangepicker/daterangepicker.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/daterangepicker/daterangepicker.zh-CN.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/datepicker/bootstrap-datepicker.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/slimScroll/jquery.slimscroll.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/fastclick/fastclick.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/iCheck/icheck.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/adminLTE/js/app.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/select2/select2.full.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-wysihtml5/bootstrap-wysihtml5.zh-CN.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/js/bootstrap-markdown.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/locale/bootstrap-markdown.zh.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/js/markdown.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/js/to-markdown.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/ckeditor/ckeditor.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/input-mask/jquery.inputmask.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/input-mask/jquery.inputmask.extensions.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/datatables/jquery.dataTables.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/chartjs/Chart.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.resize.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.pie.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.categories.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-slider/bootstrap-slider.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.min.js"></script>
<script>
$(document).ready(function() {
// 选择框
$(".select2").select2();
// WYSIHTML5编辑器
$(".textarea").wysihtml5({
locale : 'zh-CN'
});
// 全选操作
$("#selall").click(function() {
var clicks = $(this).is(':checked');
if (!clicks) {
$("#dataList td input[type='checkbox']").iCheck("uncheck");
} else {
$("#dataList td input[type='checkbox']").iCheck("check");
}
$(this).data("clicks", !clicks);
});
});
// 设置激活菜单
function setSidebarActive(tagUri) {
var liObj = $("#" + tagUri);
if (liObj.length > 0) {
liObj.parent().parent().addClass("active");
liObj.addClass("active");
}
}
function addRoles() {
var roleList=new Array();
$("input[name='roleId']:checked").each(
function () {
roleList.push($(this).val())
}
);
var userId=$("input[name=userId]").val();
alert(roleList);
alert(userId);
$.ajax({
type:"post",
url: "${pageContext.request.contextPath}/user/addRole.do",
data:{roleList:roleList.toString(),userId:userId},
success:function () {
alert("添加角色成功");
location.href="${pageContext.request.contextPath}/user/findAll.do";
},
error:function () {
alert("添加角色失败");
}
});
}
</script>
</body>
</html>
user-update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>数据 - AdminLTE2定制版</title>
<meta name="description" content="AdminLTE2定制版">
<meta name="keywords" content="AdminLTE2定制版">
<!-- Tell the browser to be responsive to screen width -->
<meta
content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"
name="viewport">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/ionicons/css/ionicons.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/iCheck/square/blue.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/morris/morris.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/jvectormap/jquery-jvectormap-1.2.2.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/datepicker/datepicker3.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/daterangepicker/daterangepicker.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/datatables/dataTables.bootstrap.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.theme.default.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/select2/select2.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/colorpicker/bootstrap-colorpicker.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/bootstrap-markdown/css/bootstrap-markdown.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/adminLTE/css/AdminLTE.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/adminLTE/css/skins/_all-skins.min.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/css/style.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.skinNice.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/bootstrap-slider/slider.css">
<link rel="stylesheet"
href="${pageContext.request.contextPath}/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.css">
</head>
<body class="hold-transition skin-purple sidebar-mini">
<div class="wrapper">
<!-- 页面头部 -->
<jsp:include page="header.jsp"></jsp:include>
<!-- 页面头部 /-->
<!-- 导航侧栏 -->
<jsp:include page="aside.jsp"></jsp:include>
<!-- 导航侧栏 /-->
<!-- 内容区域 -->
<div class="content-wrapper">
<!-- 内容头部 -->
<section class="content-header">
<h1>
用户管理 <small>添加角色表单</small>
</h1>
<ol class="breadcrumb">
<li><a href="${pageContext.request.contextPath}/index.jsp"><i
class="fa fa-dashboard"></i> 首页</a></li>
<li><a
href="#">用户管理</a></li>
<li class="active">添加角色表单</li>
</ol>
</section>
<!-- 内容头部 /-->
<form
action=""
method="post">
<!-- 正文区域 -->
<section class="content">
<%-- 隐藏域 --%>
<input type="hidden" name="userId" value="${id}">
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right: 0px">
<input id="selall"
type="checkbox" class="icheckbox_square-blue"></th>
<th class="sorting_asc">ID</th>
<th class="sorting">角色名称</th>
<th class="sorting">角色描述</th>
</tr>
</thead>
<tbody>
<c:forEach items="${roles}" var="role">
<tr>
<td>
<input name="roleId" type="checkbox" value="${role.id}">
</td>
<td>${role.id}</td>
<td>${role.rolename }</td>
<td>${role.roledesc}</td>
</tr>
</c:forEach>
</tbody>
</table>
<!--订单信息/--> <!--工具栏-->
<div class="box-tools text-center">
<button type="button" class="btn bg-maroon" onclick="addRoles()">保存</button>
<button type="button" class="btn bg-default"
onclick="history.back(-1);">返回</button>
</div>
<!--工具栏/--> </section>
<!-- 正文区域 /-->
</form>
</div>
<!-- 内容区域 /-->
</div>
<script
src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/jQueryUI/jquery-ui.min.js"></script>
<script>
$.widget.bridge('uibutton', $.ui.button);
</script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/raphael/raphael-min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/morris/morris.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/sparkline/jquery.sparkline.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/knob/jquery.knob.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/daterangepicker/moment.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/daterangepicker/daterangepicker.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/daterangepicker/daterangepicker.zh-CN.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/datepicker/bootstrap-datepicker.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/slimScroll/jquery.slimscroll.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/fastclick/fastclick.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/iCheck/icheck.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/adminLTE/js/app.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/select2/select2.full.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-wysihtml5/bootstrap-wysihtml5.zh-CN.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/js/bootstrap-markdown.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/locale/bootstrap-markdown.zh.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/js/markdown.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/js/to-markdown.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/ckeditor/ckeditor.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/input-mask/jquery.inputmask.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/input-mask/jquery.inputmask.extensions.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/datatables/jquery.dataTables.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/chartjs/Chart.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.resize.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.pie.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.categories.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.min.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-slider/bootstrap-slider.js"></script>
<script
src="${pageContext.request.contextPath}/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.min.js"></script>
<script>
$(document).ready(function() {
// 选择框
$(".select2").select2();
// WYSIHTML5编辑器
$(".textarea").wysihtml5({
locale : 'zh-CN'
});
// 全选操作
$("#selall").click(function() {
var clicks = $(this).is(':checked');
if (!clicks) {
$("#dataList td input[type='checkbox']").iCheck("uncheck");
} else {
$("#dataList td input[type='checkbox']").iCheck("check");
}
$(this).data("clicks", !clicks);
});
});
// 设置激活菜单
function setSidebarActive(tagUri) {
var liObj = $("#" + tagUri);
if (liObj.length > 0) {
liObj.parent().parent().addClass("active");
liObj.addClass("active");
}
}
function addRoles() {
var roleList=new Array();
$("input[name='roleId']:checked").each(
function () {
roleList.push($(this).val())
}
);
var userId=$("input[name=userId]").val();
alert(roleList);
alert(userId);
$.ajax({
type:"post",
url: "${pageContext.request.contextPath}/user/addRole.do",
data:{roleList:roleList.toString(),userId:userId},
success:function () {
alert("添加角色成功");
location.href="${pageContext.request.contextPath}/user/findAll.do";
},
error:function () {
alert("添加角色失败");
}
});
}
</script>
</body>
</html>
以上就是今天的全部内容,跟着老师来也不是很难,加油。