使用springmvc实现增加用户功能
现在实现新增用户的功能
UserController.java
package cn.smbms.controller; import java.util.Date; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import cn.smbms.pojo.Role; import cn.smbms.pojo.User; import cn.smbms.service.role.RoleService; import cn.smbms.service.user.UserService; import cn.smbms.tools.Constants; import cn.smbms.tools.PageSupport; @Controller @RequestMapping("/user") public class UserController{ private Logger logger = Logger.getLogger(UserController.class); @Resource private UserService userService; @Resource private RoleService roleService; @RequestMapping(value="/login.html") public String login(){ logger.debug("UserController welcome SMBMS=================="); return "login"; } @RequestMapping(value="/dologin.html",method=RequestMethod.POST) public String doLogin(@RequestParam String userCode,@RequestParam String userPassword,HttpServletRequest request,HttpSession session){ logger.debug("doLogin===================================="); //调用service方法,进行用户匹配 User user = userService.login(userCode,userPassword); if(null != user){//登录成功 //放入session session.setAttribute(Constants.USER_SESSION, user); //页面跳转(frame.jsp) return "redirect:/user/main.html"; //response.sendRedirect("jsp/frame.jsp"); }else{ //页面跳转(login.jsp)带出提示信息--转发 request.setAttribute("error", "用户名或密码不正确"); return "login"; } } @RequestMapping(value="/main.html") public String main(HttpSession session){ if(session.getAttribute(Constants.USER_SESSION) == null){ return "redirect:/user/login.html"; } return "frame"; } @RequestMapping(value="/logout.html") public String logout(HttpSession session){ //清除session session.removeAttribute(Constants.USER_SESSION); return "login"; } @RequestMapping(value="/exlogin.html",method=RequestMethod.GET) public String exLogin(@RequestParam String userCode,@RequestParam String userPassword){ logger.debug("exLogin===================================="); //调用service方法,进行用户匹配 User user = userService.login(userCode,userPassword); if(null == user){//登录失败 throw new RuntimeException("用户名或者密码不正确!"); } return "redirect:/user/main.html"; } /*@ExceptionHandler(value={RuntimeException.class}) public String handlerException(RuntimeException e,HttpServletRequest req){ req.setAttribute("e", e); return "error"; }*/ @RequestMapping(value="/userlist.html") public String getUserList(Model model, @RequestParam(value="queryname",required=false) String queryUserName, @RequestParam(value="queryUserRole",required=false) String queryUserRole, @RequestParam(value="pageIndex",required=false) String pageIndex){ logger.info("getUserList ---- > queryUserName: " + queryUserName); logger.info("getUserList ---- > queryUserRole: " + queryUserRole); logger.info("getUserList ---- > pageIndex: " + pageIndex); int _queryUserRole = 0; List<User> userList = null; //设置页面容量 int pageSize = Constants.pageSize; //当前页码 int currentPageNo = 1; if(queryUserName == null){ queryUserName = ""; } if(queryUserRole != null && !queryUserRole.equals("")){ _queryUserRole = Integer.parseInt(queryUserRole); } if(pageIndex != null){ try{ currentPageNo = Integer.valueOf(pageIndex); }catch(NumberFormatException e){ return "redirect:/user/syserror.html"; //response.sendRedirect("syserror.jsp"); } } //总数量(表) int totalCount = userService.getUserCount(queryUserName,_queryUserRole); //总页数 PageSupport pages=new PageSupport(); pages.setCurrentPageNo(currentPageNo); pages.setPageSize(pageSize); pages.setTotalCount(totalCount); int totalPageCount = pages.getTotalPageCount(); //控制首页和尾页 if(currentPageNo < 1){ currentPageNo = 1; }else if(currentPageNo > totalPageCount){ currentPageNo = totalPageCount; } userList = userService.getUserList(queryUserName,_queryUserRole,currentPageNo,pageSize); model.addAttribute("userList", userList); List<Role> roleList = null; roleList = roleService.getRoleList(); model.addAttribute("roleList", roleList); model.addAttribute("queryUserName", queryUserName); model.addAttribute("queryUserRole", queryUserRole); model.addAttribute("totalPageCount", totalPageCount); model.addAttribute("totalCount", totalCount); model.addAttribute("currentPageNo", currentPageNo); return "userlist"; } @RequestMapping(value="/syserror.html") public String sysError(){ return "syserror"; } @RequestMapping(value="/useradd.html",method=RequestMethod.GET) public String addUser(@ModelAttribute("user") User user){ return "useradd"; } /* @RequestMapping(value="/useradd.html",method=RequestMethod.GET) public String addUser(User user,Model model){ model.addAttribute("user", user); return "useradd"; }*/ @RequestMapping(value="/useraddsave.html",method=RequestMethod.POST) public String addUserSave(User user,HttpSession session){ user.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId()); user.setCreationDate(new Date()); if(userService.add(user)){ return "redirect:/user/userlist.html"; } return "useradd"; } }
userlist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@include file="/WEB-INF/jsp/common/head.jsp"%> <div class="right"> <div class="location"> <strong>你现在所在的位置是:</strong> <span>用户管理页面</span> </div> <div class="search"> <form method="post" action="${pageContext.request.contextPath }/user/userlist.html"> <input name="method" value="query" class="input-text" type="hidden"> <span>用户名:</span> <input name="queryname" class="input-text" type="text" value="${queryUserName }"> <span>用户角色:</span> <select name="queryUserRole"> <c:if test="${roleList != null }"> <option value="0">--请选择--</option> <c:forEach var="role" items="${roleList}"> <option <c:if test="${role.id == queryUserRole }">selected="selected"</c:if> value="${role.id}">${role.roleName}</option> </c:forEach> </c:if> </select> <input type="hidden" name="pageIndex" value="1"/> <input value="查 询" type="submit" id="searchbutton"> <a href="${pageContext.request.contextPath}/user/useradd.html" >添加用户</a> </form> </div> <!--用户--> <table class="providerTable" cellpadding="0" cellspacing="0"> <tr class="firstTr"> <th width="10%">用户编码</th> <th width="20%">用户名称</th> <th width="10%">性别</th> <th width="10%">年龄</th> <th width="10%">电话</th> <th width="10%">用户角色</th> <th width="30%">操作</th> </tr> <c:forEach var="user" items="${userList }" varStatus="status"> <tr> <td> <span>${user.userCode }</span> </td> <td> <span>${user.userName }</span> </td> <td> <span> <c:if test="${user.gender==1}">男</c:if> <c:if test="${user.gender==2}">女</c:if> </span> </td> <td> <span>${user.age}</span> </td> <td> <span>${user.phone}</span> </td> <td> <span>${user.userRoleName}</span> </td> <td> <span><a class="viewUser" href="javascript:;" userid=${user.id } username=${user.userName }><img src="${pageContext.request.contextPath }/statics/images/read.png" alt="查看" title="查看"/></a></span> <span><a class="modifyUser" href="javascript:;" userid=${user.id } username=${user.userName }><img src="${pageContext.request.contextPath }/statics/images/xiugai.png" alt="修改" title="修改"/></a></span> <span><a class="deleteUser" href="javascript:;" userid=${user.id } username=${user.userName }><img src="${pageContext.request.contextPath }/statics/images/schu.png" alt="删除" title="删除"/></a></span> </td> </tr> </c:forEach> </table> <input type="hidden" id="totalPageCount" value="${totalPageCount}"/> <c:import url="rollpage.jsp"> <c:param name="totalCount" value="${totalCount}"/> <c:param name="currentPageNo" value="${currentPageNo}"/> <c:param name="totalPageCount" value="${totalPageCount}"/> </c:import> </div> </section> <!--点击删除按钮后弹出的页面--> <div class="zhezhao"></div> <div class="remove" id="removeUse"> <div class="removerChid"> <h2>提示</h2> <div class="removeMain"> <p>你确定要删除该用户吗?</p> <a href="#" id="yes">确定</a> <a href="#" id="no">取消</a> </div> </div> </div> <%@include file="/WEB-INF/jsp/common/foot.jsp" %> <script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/userlist.js"></script>
User.java
运行结果: