美文网首页软件测试学习之路
SSM 框架实现自定义缓存管理

SSM 框架实现自定义缓存管理

作者: 乘风破浪的姐姐 | 来源:发表于2018-05-07 14:26 被阅读31次

    在使用SSM框架,将数据库中某个表的数据展示到列表中时,经常会有多表关联,需要展示其它表中的字段,而当前表中只存储有其他表中某字段的id。
    例如:用户与角色的关系,测试用例与系统模块的关系等。
    下列场景表的关系如图:


    image.png image.png

    现需要展示用户列表、测试用例列表。具体解决方案如下:
    1、新增缓存监听类:CacheLilter
    首先,拿到具体需要监听的实体bean,WebApplicationContextUtils.getWebApplicationContext(se.getServletContext()).getBean(ICaseSystemService.class)
    然后,在spring容器启动时,监听所有加载的类中是否有指定要缓存的实体类。如果有,则加入到list中。
    当有多个实体类需要缓存时,就需要多个list。

    package com.sc.listener;
    
    import com.sc.model.CaseSystem;
    import com.sc.model.Role;
    import com.sc.service.ICaseSystemService;
    import com.sc.service.IRoleService;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import java.util.List;
    
    public class CacheLilter implements ServletContextListener {
        public void contextInitialized(ServletContextEvent se){
            ICaseSystemService systemService = (ICaseSystemService) WebApplicationContextUtils.getWebApplicationContext(se.getServletContext()).getBean(ICaseSystemService.class);
    
            List<CaseSystem> list =systemService.getAll();
            for(CaseSystem caseSystem:list){
                ObjectCache.addCache(caseSystem.getSystemid(),caseSystem);
            }
    
            IRoleService roleService = (IRoleService) WebApplicationContextUtils.getWebApplicationContext(se.getServletContext()).getBean(IRoleService.class);
            List<Role> list1 =roleService.getAll();
            for(Role role:list1){
                ObjectCache.addCache(role.getRoleid(),role);
            }
        }
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
        }
    }
    

    2、新增缓存具体实现类ObjectCache

    package com.sc.listener;
    
    import com.sc.model.CaseSystem;
    import com.sc.model.Role;
    
    import java.util.*;
    
    public class ObjectCache {
        private static Map<String,Object> map =new HashMap<String,Object>();
        public static void addCache(String key,Object value){
            map.put(key,value);
        }
        public static Object get(String key){
            if(map.containsKey(key)){
              return   map.get(key);
            }
            return null;
        }
        public static void deleteCache(String key){
            if(map.containsKey(key)){
                map.remove(key);
            }
        }
    
        public static List getAllObject(){
            List  list = new ArrayList();
            Set<String> set = map.keySet();
            for(String s : set){
                Object object = map.get(s);
                if(object instanceof CaseSystem){
                    list.add((CaseSystem) object);
                }
                if( object instanceof Role){
                    list.add((Role) object);
                }
            }
            return list;
        }
    
        public static String getName(String id){
            Object object = get(id);
            if(object!=null && object instanceof CaseSystem){
                 return  ((CaseSystem) object).getSystemname();
            }
            if(object!=null && object instanceof Role){
                return  ((Role) object).getRolename();
            }
            return "";
        }
    }
    

    3、各个需要缓存的实体类对应的接口服务类需要添加 getAll()方法。
    例如IRoleService接口:

    public interface IRoleService {
         Map<String,Object> rolelist(Integer pageNum, Integer pageSize);
         int addrole(Role role,String str);
         List<Role> toaddrole();
         int deleterole(String roleid);
         int updaterole(Role role);
         Map<String,Object> toupdaterole(String roleid);
         String check(String rolename);
         List<Role> getAll();
    }
    

    在要使用关联字段的接口服务中添加getRole()方法
    例如IUserService 接口:

    public interface IUserService {
         Map<String,Object> userlist(Integer pageNum, Integer pageSize);
         int adduser(User user);
         List<User> toadduser();
         int deleteuser(String userid);
         int updateuser(User user);
         Map<String,Object> toupdateuser(String userid);
         String check(String username);
         User getUser(String userid);
         List<Role> getRole();
    
    }
    

    4、接口实现类中实现getAll()方法
    RoleServiceImpl 实现类:

    package com.sc.impl;
    
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import com.sc.dao.FuncRoleMapper;
    import com.sc.dao.FunctionMapper;
    import com.sc.dao.RoleMapper;
    import com.sc.model.*;
    import com.sc.service.IFunctionService;
    import com.sc.service.IRoleService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;
    
    @Service
    public class RoleServiceImpl implements IRoleService {
    
        @Autowired
        private RoleMapper roleMapper;
    
        @Autowired
        private FuncRoleMapper funcRoleMapper;
    
        @Autowired
        private FunctionMapper functionMapper;
    
        @Override
        public Map<String, Object> rolelist(Integer pageNum, Integer pageSize) {
            if(pageNum==null){
                pageNum=1;
            }
            if(pageSize==null || pageSize==0){
                pageSize=10;
            }
    
            PageHelper.startPage(pageNum,pageSize);
    
            RoleCriteria rc = new RoleCriteria();
            rc.createCriteria();
            List<Role> roleList = roleMapper.selectByExample(rc);
            PageInfo<Role> pageInfo = new PageInfo<Role>(roleList);
    
            Map<String,Object> data = new HashMap<String,Object>();
    
            data.put("totalPages",pageInfo.getPages());
            data.put("pageNum",pageInfo.getPageNum());
            data.put("pageSize",pageInfo.getPageSize());
            data.put("roleList",pageInfo.getList());
            return data;
        }
    
        @Override
        public int addrole(Role role,String str) {
            role.setRoleid(UUID.randomUUID().toString());
            roleMapper.insert(role);
    
            RoleCriteria roleCriteria = new RoleCriteria();
            roleCriteria.createCriteria().andRolenameEqualTo(role.getRolename()).andDescriptionEqualTo(role.getDescription());
            List<Role> roles = roleMapper.selectByExample(roleCriteria);
            role = roles.get(0);
    
            String [] strs = {};
            if(str!=null && !str.equals("")){
                if(str.length()>0){
                    if(str.contains(",")){
                        strs = str.split(",");
                    }else{
                        if(!str.contains(",")){
                            strs[0]=str;
                        }
                    }
                }
            }
            if(strs.length>0 && strs!=null){
                for(String s : strs){
                    FuncRole funcRole = new FuncRole();
                    funcRole.setFuncroleid(UUID.randomUUID().toString());
                    funcRole.setFuncid(s);
                    funcRole.setRoleid(role.getRoleid());
                    funcRoleMapper.insert(funcRole);
                }
            }
            return 0;
        }
    
        @Override
        public List<Role> toaddrole() {
            RoleCriteria rc = new RoleCriteria();
            rc.createCriteria();
            List<Role> roleList = roleMapper.selectByExample(rc);
            return roleList;
        }
    
        @Override
        public int deleterole(String roleid) {
            return  roleMapper.deleteByPrimaryKey(roleid);
        }
    
        @Override
        public int updaterole(Role role) {
            Role oldrole = roleMapper.selectByPrimaryKey(role.getRoleid());
            oldrole.setRolename(role.getRolename());
            oldrole.setDescription(role.getDescription());
            oldrole.setStatus(role.getStatus());
            return roleMapper.updateByPrimaryKey(oldrole);
        }
    
        @Override
        public Map<String, Object> toupdaterole(String roleid) {
            Map<String, Object> map = new HashMap<String,Object>();
            FunctionCriteria functionCriteria = new FunctionCriteria();
            functionCriteria.createCriteria();
            List<Function> functions = functionMapper.selectByExample(functionCriteria);
    
            Role role = roleMapper.selectByPrimaryKey(roleid);
    
            FuncRoleCriteria funcRoleCriteria = new FuncRoleCriteria();
            funcRoleCriteria.createCriteria().andRoleidEqualTo(role.getRoleid());
            List<FuncRole> funcRoles =funcRoleMapper.selectByExample(funcRoleCriteria);
    
            map.put("functions",functions);
            map.put("role",role);
            map.put("funcRoles",funcRoles);
    
            return map;
        }
    
        @Override
        public String check(String rolename) {
            RoleCriteria rc = new RoleCriteria();
            rc.createCriteria().andRolenameEqualTo(rolename);
            List<Role> roles = roleMapper.selectByExample(rc);
            String s = "";
            if(roles.size()>0){
                s="Y";
            }else{
                s="N";
            }
            return s;
        }
    
        @Override
        public List<Role> getAll() {
          return roleMapper.selectByExample(null);
        }
    }
    

    UserServiceImpl实现类:

    package com.sc.impl;
    
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import com.sc.dao.RoleMapper;
    import com.sc.dao.UserMapper;
    import com.sc.listener.ObjectCache;
    import com.sc.model.Role;
    import com.sc.model.RoleCriteria;
    import com.sc.model.User;
    import com.sc.model.UserCriteria;
    import com.sc.service.IUserService;
    import com.sc.utils.MD5CryptUtil;
    import org.apache.commons.codec.digest.DigestUtils;
    import org.apache.commons.codec.digest.Md5Crypt;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import sun.security.provider.MD5;
    
    import java.util.*;
    
    @Service
    public class UserServiceImpl implements IUserService {
    
        @Autowired
        UserMapper userMapper;
        @Autowired
        RoleMapper roleMapper;
    
        @Override
        public Map<String, Object> userlist(Integer pageNum, Integer pageSize) {
            if(pageNum==null){
                pageNum=1;
            }
            if(pageSize==null || pageSize==0){
                pageSize=10;
            }
            PageHelper.startPage(pageNum,pageSize);
            Map<String,Object> map = new HashMap<String,Object>();
            UserCriteria uc = new UserCriteria();
            List<User> userList = userMapper.selectByExample(uc);
            PageInfo<User> pageInfo = new PageInfo<User>(userList);
    
            map.put("objs",pageInfo.getList());
            map.put("pag",pageInfo.getPageNum());
            map.put("pagesize",pageInfo.getPageSize());
            map.put("total",pageInfo.getTotal());
    
            return map;
        }
    
        @Override
        public int adduser(User user) {
    
           user.setUserid(UUID.randomUUID().toString());
           user.setPassword(Md5Crypt.md5Crypt(user.getPassword().getBytes()));
           // user.setPassword(DigestUtils.md5Hex(user.getPassword()+user.getUserid()));
            return  userMapper.insert(user);
        }
    
        @Override
        public List<User> toadduser() {
            return null;
        }
    
        @Override
        public int deleteuser(String userid) {
            return userMapper.deleteByPrimaryKey(userid);
        }
    
        @Override
        public int updateuser(User user) {
            User olduser =userMapper.selectByPrimaryKey(user.getUserid());
            olduser.setAddr(user.getAddr());
            olduser.setBianhao(user.getBianhao());
            olduser.setDescription(user.getDescription());
            olduser.setCode(user.getCode());
            olduser.setEmail(user.getEmail());
            olduser.setName(user.getName());
            olduser.setSex(user.getSex());
            olduser.setStatus(user.getStatus());
            olduser.setRoleid(user.getRoleid());
            olduser.setPassword(Md5Crypt.md5Crypt(user.getPassword().getBytes()));
            return userMapper.updateByPrimaryKey(olduser);
        }
    
        @Override
        public Map<String, Object> toupdateuser(String userid) {
            Map<String,Object> map = new HashMap<String,Object>();
            if(userid.length()>0&&userid!=null){
               User user = userMapper.selectByPrimaryKey(userid);
               map.put("user",user);
            }
    
            RoleCriteria rc = new RoleCriteria();
            rc.createCriteria();
            List<Role> roles = roleMapper.selectByExample(rc);
            map.put("roles",roles);
            return map;
        }
    
        @Override
        public String check(String username) {
            UserCriteria uc = new UserCriteria();
            uc.createCriteria().andUsernameEqualTo(username);
            List<User> list =userMapper.selectByExample(uc);
            String s=null;
            if(list.size()>0 && list!=null){
                s="Y";
            }else{
                s="N";
            }
            return s;
        }
    
        @Override
        public User getUser(String userid) {
            return userMapper.selectByPrimaryKey(userid);
        }
    
        @Override
        public List<Role> getRole() {
            List<Object> objects = ObjectCache.getAllObject();
            List<Role> roles = new ArrayList<Role>();
            for(Object object:objects){
                if(object instanceof Role){
                    roles.add((Role) object);
                }
            }
            return roles;
        }
    }
    

    5、在实体model中,添加列表中需要显示的关联id对应的名称字段,并实现其get方法。
    private String rolename;
    public String getRolename(){
    return ObjectCache.getName(this.roleid);
    }
    具体User.class如下:

    package com.sc.model;
    
    import com.sc.listener.ObjectCache;
    
    import java.io.Serializable;
    
    public class User implements Serializable {
        private String userid;
    
        private String username;
    
        private String password;
    
        private String name;
    
        private String sex;
    
        private String addr;
    
        private String roleid;
    
        private String rolename;
    
        private String description;
    
        private String bianhao;
    
        private Integer status;
    
        private String email;
    
        private String code;
    
        private static final long serialVersionUID = 1L;
    
        public String getUserid() {
            return userid;
        }
    
        public void setUserid(String userid) {
            this.userid = userid == null ? null : userid.trim();
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username == null ? null : username.trim();
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password == null ? null : password.trim();
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name == null ? null : name.trim();
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex == null ? null : sex.trim();
        }
    
        public String getAddr() {
            return addr;
        }
    
        public void setAddr(String addr) {
            this.addr = addr == null ? null : addr.trim();
        }
    
        public String getRoleid() {
            return roleid;
        }
    
        public void setRoleid(String roleid) {
            this.roleid = roleid == null ? null : roleid.trim();
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description == null ? null : description.trim();
        }
    
        public String getBianhao() {
            return bianhao;
        }
    
        public void setBianhao(String bianhao) {
            this.bianhao = bianhao == null ? null : bianhao.trim();
        }
    
        public Integer getStatus() {
            return status;
        }
    
        public void setStatus(Integer status) {
            this.status = status;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email == null ? null : email.trim();
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code == null ? null : code.trim();
        }
    
        public String getRolename(){
            return ObjectCache.getName(this.roleid);
        }
    
        @Override
        public boolean equals(Object that) {
            if (this == that) {
                return true;
            }
            if (that == null) {
                return false;
            }
            if (getClass() != that.getClass()) {
                return false;
            }
            User other = (User) that;
            return (this.getUserid() == null ? other.getUserid() == null : this.getUserid().equals(other.getUserid()))
                && (this.getUsername() == null ? other.getUsername() == null : this.getUsername().equals(other.getUsername()))
                && (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword()))
                && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
                && (this.getSex() == null ? other.getSex() == null : this.getSex().equals(other.getSex()))
                && (this.getAddr() == null ? other.getAddr() == null : this.getAddr().equals(other.getAddr()))
                && (this.getRoleid() == null ? other.getRoleid() == null : this.getRoleid().equals(other.getRoleid()))
                && (this.getDescription() == null ? other.getDescription() == null : this.getDescription().equals(other.getDescription()))
                && (this.getBianhao() == null ? other.getBianhao() == null : this.getBianhao().equals(other.getBianhao()))
                && (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus()))
                && (this.getEmail() == null ? other.getEmail() == null : this.getEmail().equals(other.getEmail()))
                && (this.getCode() == null ? other.getCode() == null : this.getCode().equals(other.getCode()));
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((getUserid() == null) ? 0 : getUserid().hashCode());
            result = prime * result + ((getUsername() == null) ? 0 : getUsername().hashCode());
            result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode());
            result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
            result = prime * result + ((getSex() == null) ? 0 : getSex().hashCode());
            result = prime * result + ((getAddr() == null) ? 0 : getAddr().hashCode());
            result = prime * result + ((getRoleid() == null) ? 0 : getRoleid().hashCode());
            result = prime * result + ((getDescription() == null) ? 0 : getDescription().hashCode());
            result = prime * result + ((getBianhao() == null) ? 0 : getBianhao().hashCode());
            result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode());
            result = prime * result + ((getEmail() == null) ? 0 : getEmail().hashCode());
            result = prime * result + ((getCode() == null) ? 0 : getCode().hashCode());
            return result;
        }
    }
    

    6、在UserController类中可进行使用。

    package com.sc.controller;
    
    import com.sc.dao.UserMapper;
    import com.sc.listener.ObjectCache;
    import com.sc.model.Role;
    import com.sc.model.User;
    import com.sc.service.IUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    @Controller
    @RequestMapping(value = "/user")
    public class UserController {
        @Autowired
        IUserService userService;
        @Autowired
        HttpServletRequest request;
    
        @RequestMapping(value = "/userlist.do")
        public String userList(Integer pageNum, Integer pageSize){
            Map<String,Object> map = userService.userlist(pageNum,pageSize);
            request.setAttribute("data",map);
            return "user/userList";
        }
    
        @RequestMapping(value = "/toadduser.do")
        public String toAddUser(){
            request.setAttribute("rolenames",userService.getRole());
            return "user/addUser";
        }
    
        @RequestMapping(value = "/adduser.do")
        public String addUser(@ModelAttribute("user") User user){
            userService.adduser(user);
            return "redirect:userlist.do";
        }
        @RequestMapping(value = "/toupdateuser.do")
        public String toUpdateUser(String userid){
            User user = userService.getUser(userid);
            request.setAttribute("user",user);
            request.setAttribute("rolenames",userService.getRole());
            return "user/updateUser";
        }
    
        @RequestMapping(value = "/updateuser.do")
        public String updateUser(@ModelAttribute("user") User user){
            userService.updateuser(user);
            return "redirect:userlist.do";
        }
    
        @RequestMapping(value = "/deleteuser.do")
        public String deleteUser(String userid){
            userService.deleteuser(userid);
            return "redirect:userlist.do";
        }
    
        @RequestMapping(value = "/checkusername.do")
        @ResponseBody
        public String check(String username){
           return userService.check(username);
        }
    }
    

    7、在具体jsp页面可使用jstl表达式进行引用
    用户列表页面,需要显示用户对应的角色名称

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
        <base href="<%=basePath%>">
    
        <title>用户管理界面</title>
    
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <link rel="stylesheet" type="text/css" href="<%=path %>/css/style.css"/>
        <link href="<%=path %>/css/houtai.css" rel="stylesheet"/>
        <script type="text/javascript" src="<%=path %>/js/DD_belatedPNG_0.0.8a-min.js"></script>
        <script type="text/javascript" src="<%=path %>/js/jquery-3.3.1.min.js"></script>
    
        <script type="text/javascript">
            function adduser() {
                $.post("user/toadduser.do", {}, function (data) {
                    $("#adduser").show();
                    $("#adduser").html(data).show();
                });
            }
    
            function updateuser(v) {
                $.post("user/toupdateuser.do", {
                    "userid": v
                }, function (data) {
                    $("#adduser").show();
                    $("#adduser").html(data).show();
                });
            }
    
            function hiden() {
                $("#adduser").hide();//hide()函数,实现隐藏,括号里还可以带一个时间参数(毫秒)例如hide(2000)以2000毫秒的速度隐藏,还可以带slow,fast
            }
    
            function toPage(p) {
                var total = ${data.total};
                if (p <= 0 || p > total) {
                    return;
                }
                document.frm.action = "user/userlist.do";
                document.getElementById("pag").value = p;
                document.frm.submit();
            }
    
            function submitForm() {
                document.frm.action = "user/userlist.do";
                document.getElementById("pag").value = 0;
                document.frm.submit();
            }
        </script>
    
    </head>
    
    <body>
    <form name="frm" action="" method="get">
    
        <div class="tai_right" id="tai_right">
            <table width="100%" class="table_da">
                <tr></tr>
                <tr class="back_tit">
                    <td style="width:15%;">编号</td>
                    <td style="width:10%;">用户名</td>
                    <td style="width:10%;">角色</td>
                    <td style="width:10%;">姓名</td>
                    <td style="width:10%;">性别</td>
                    <td style="width:25%;">描述</td>
                    <td style="width:20%;"><a href="javascript:void(0)" onclick="adduser()"><font color="#fc7305"><b>添加</b></font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;操作
                    </td>
                </tr>
                <c:forEach items="${data.objs}" var="o" varStatus="status">
                    <tr class="hang01">
                        <td>
                                ${status.count}
                        </td>
                        <td>
                                ${o.username }
                        </td>
                        <td>
                                ${o.rolename}
                        </td>
                        <td>
                                ${o.name }
                        </td>
                        <td>
                                ${o.sex }
                        </td>
                        <td style="text-align:left; line-height:25px; padding:0 7px;">
                            &nbsp;${o.description }
                        </td>
                        <td class="w_190">
                            <input type="button" class="gaic" value="修改" onclick="updateuser('${o.userid }')"/>
                            <input type="button" class="gaic" value="删除"
                                   onclick="window.location.href='<%=path %>/user/deleteuser.do?userid=${o.userid }'"/>
                                <%-- <a href="user/delete.do?userid=${o.userid }">
                                </a> --%>
                        </td>
                    </tr>
                </c:forEach>
            </table>
            <br>
            <table border=0 align="center" width="90%">
                <tr>
                    <td width="50%">
                        每页显示 <select name="pagesize" style="font-size:18px;70px;height:25px;"
                                     onChange="javascript:submitForm(${data.pag});">
                        <option value="10" <c:if test="${data.pagesize==10 }">selected</c:if>>10</option>
                        <option value="20" <c:if test="${data.pagesize==20 }">selected</c:if>>20</option>
                        <option value="50" <c:if test="${data.pagesize==50 }">selected</c:if>>50</option>
                        <option value="100" <c:if test="${data.pagesize==100 }">selected</c:if>>100</option>
                    </select></td>
                    <td width="50%" align="right">
                        <a href="javascript:toPage(${data.pag-1 });" class="gaic">上一页</a>&nbsp;&nbsp;
                        <a href="javascript:toPage(${data.pag+1 });" class="gaic">下一页</a>
                        &nbsp;第<font color="red">${data.pag}<font><font color="black">页&nbsp;共${data.total }页</font>&nbsp;&nbsp;
                    </font></td>
                </tr>
    
            </table>
            <input type="hidden" name="pag" id="pag" value="${data.pag}">
        </div>
    </form>
    <div id="adduser" style="position:absolute;top:150px;left: 200px;"></div>
    </body>
    </html>
    

    新增用户页面,需要使用角色列表

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
        <link href="<%=path %>/css/houtai.css" rel="stylesheet" />
        <script type="text/javascript" src="<%=path %>/js/iepng.js"></script>
    <script type="text/javascript" src="<%=path %>/js/jquery-3.3.1.min.js"></script>
        <script type="text/javascript">
        DD_belatedPNG.fix('div, ul, img, li, input,p,ul,ol,h1,h2,h3,a,span,i'); 
            var aa=false,bb=false,cc=false,dd=false,ee=false,ff=false,gg=false,ee=false;
            function checkdulname(a){
                if(a.length>0){
                    aa=true;
                    $("#checkusername_error").html("");
                    $.post("user/checkusername.do",{
                        "username":a
                    },function(data){
                        if(data=="Y"||data=="\"Y\""){
                            bb=true;
                        }else{
                            $("#checkusername_error").html("<font color=red>用户名已存在!</font>");
                            bb=false;
                        }
                    });
                }else{
                    $("#checkusername_error").html("<font color=red>请填写用户名!</font>");
                    aa=false;
                }
            }
            function checknotnull2(e){
                if(e.length>0){
                    $("#password_error").html("");
                    cc=true;
                }else{
                    $("#password_error").html("<font color=red>请填写密码!</font>"); 
                    cc=false;       
                }
            }
            function checknotnullb(e){
                if(e.length>0){
                    $("#passwordb_error").html("");
                    ee=true;
                }else{
                    $("#passwordb_error").html("<font color=red>请填写工号!</font>");    
                    ee=false;       
                }
            }
            function checknotnull3(e){
                if(e.length>0){
                    $("#usersname_error").html("");
                    dd=true;
                }else{
                    $("#usersname_error").html("<font color=red>请填写姓名!</font>");
                    dd=false;
                }
            }
            function checknotnull4(e){
                if(e.length>0){
                    $("#usersex_error").html("");
                    ee=true;
                }else{
                    $("#usersex_error").html("<font color=red>请填写性别!</font>");
                    ee=false;
                }
            }
            
            function checknotnull5(e){
                if(e.length>0){
                    $("#useraddr_error").html("");
                    ff=true;
                }else{
                    $("#useraddr_error").html("<font color=red>请填写地址!</font>");
                    ff=false;
                }
            }
            function checknotnull6(e){
                if(e.length>0){
                    $("#description_error").html("");
                    gg=true;
                }else{
                    $("#description_error").html("<font color=red>请填写描述!</font>");
                    gg=false;
                }
            }
            function ccc(){
                if(aa&&bb&&cc&&dd&&ee&&gg&&ff){
                    return true;
                }else{
                    alert("数据填写不合法!");
                    return false;
                }
            }
        </script>
        <div id="tanchuk">
      <div class="tan_top"></div>
      <div class="tan_main">
      <form action="user/adduser.do" method="post">
        <table class="tck" width="213" border="0" cellspacing="0" cellpadding="0">
            <tr bgcolor=white><td align=right>工&nbsp;&nbsp;号:</td><td>
            <input name="bianhao" class="text_yhm" onblur="checknotnullb(this.value)"/><span id="passwordb_error"></span>
            </td></tr>
            <tr bgcolor=white><td align=right class="tc_w108">用户名:</td><td>
            <input class="text_yhm" name="username" onblur="checkdulname(this.value)" /><span id="checkusername_error"></span>
            </td></tr>
            <tr bgcolor=white><td align=right>密&nbsp;&nbsp;码:</td><td>
            <input name="password" type="password" class="text_yhm" onblur="checknotnull2(this.value)"/><span id="password_error"></span>
            </td></tr>
            <tr bgcolor=white><td align=right>姓&nbsp;&nbsp;名:</td><td>
            <input name="name" class="text_yhm" onblur="checknotnull3(this.value)"/><span id="usersname_error"></span>
            </td></tr>
            <tr bgcolor=white><td align=right>性&nbsp;&nbsp;别:</td><td>
            <input name="sex" class="text_yhm" onblur="checknotnull4(this.value)"/><span id="usersex_error"></span>
            </td></tr>
            <tr bgcolor=white><td align=right>地&nbsp;&nbsp;址:</td><td>
            <input name="addr" class="text_yhm" onblur="checknotnull5(this.value)"/><span id="useraddr_error"></span>
            </td></tr>
            <tr bgcolor=white><td align=right>描&nbsp;&nbsp;述:</td><td>
            <input name="description" class="text_yhm" onblur="checknotnull6(this.value)"/><span id="description_error"></span>
            </td></tr>
            <tr bgcolor=white><td align=right>角&nbsp;&nbsp;色:</td><td>
            <select name="roleid">
                <c:forEach items="${rolenames}" var="r">
                    <option value="${r.roleid }">${r.rolename}</option>
                </c:forEach>
            </select>
            </td></tr>
            <tr bgcolor=white><td colspan="2" align=center>
                <input type="submit" value="确定" class="queren"/><input type="button" value="取消" class="quxiao" onclick="hiden()"/>
            </td></tr>
        </table>
    </form>
    </div>
    <div class="tan_bottom"></div>
    </div>
    

    效果图如下:


    image.png
    image.png

    相关文章

      网友评论

        本文标题:SSM 框架实现自定义缓存管理

        本文链接:https://www.haomeiwen.com/subject/gawmrftx.html