1、框架
strusts2 + Hibernate + spring
2、图片
图1-1 网站结构
图1-2 java代码结构
3、源代码
3.1 UserAction.java
package com.xhu.action; import java.util.HashMap; import java.util.Map; import javax.annotation.Resource; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import org.apache.struts2.interceptor.SessionAware; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.opensymphony.xwork2.ActionSupport; import com.xhu.DTO.PwdDto; import com.xhu.model.Place; import com.xhu.model.User; import com.xhu.model.UserType; import com.xhu.model.PageResultSet; import com.xhu.service.PlaceManager; import com.xhu.service.UserManager; import com.xhu.util.MD5; @Component("userAction") @Scope("prototype") public class UserAction extends ActionSupport implements SessionAware { /** * */ private static final long serialVersionUID = 2566144909692829328L; /** * */ private UserManager userManager; private User user; private String province; private String city; private String county; private PlaceManager placeManager; private final String NOCHOOSE = "--请选择--"; private static final int pageSize = 10; private int currentPage; // 接受传递的页码参数 private PageResultSet<User> pageResultSet; private String keyWord; // 分页查询时的关键字 private Map<String, Object> session; private String prePage; // 登录前要访问的页面 // 返回的字符串提示信息 private String returnStr; private String userPwd;// 密码 private String jsonStr;// 接受从视图传过来的json字符串 private long userId; // 用户的流水号 private int myPageSize = 5; // 从视图中传过来的pageSize, 默认值为5 /** * 修改用户密码 * * @return */ public String changePwd() { PwdDto pwdDto = null; int result = -1; try { JSONObject js = JSONObject.fromObject(jsonStr); pwdDto = (PwdDto) JSONObject.toBean((JSONObject) js, PwdDto.class); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if (pwdDto != null) { try { result = this.userManager.doChangePwd(pwdDto.getUserId(), pwdDto.getOldPwd(), pwdDto.getNewPwd()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } Map<String, String> myMap = new HashMap<String, String>(); myMap.put("changePwdResult", "" + result); JSONObject js = JSONObject.fromObject(myMap); this.returnStr = js.toString(); return SUCCESS; } /** * 显示新增用户 * * @return */ public String ajaxNewFixed() { try { this.pageResultSet = this.userManager.newAddFixed(myPageSize, this.currentPage); Map<String, Object> myMap = new HashMap<String, Object>(); myMap.put("currentPage", "" + this.pageResultSet.getPageInfo().getCurrentPage()); myMap.put("totalPage", "" + this.pageResultSet.getPageInfo().getTotalPage()); myMap.put("list", this.pageResultSet.getList()); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[] { "cars" }); JSONObject js = JSONObject.fromObject(myMap, jsonConfig); returnStr = js.toString(); // System.out.println("Info: userAction - ajaxFenyeQuery - returnStr:" // + returnStr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; } /** * 显示用户信息 * * @return */ public String show() { try { this.user = this.userManager.getUserById(userId); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; } public String personnalInfo() { String strUserId = (String) session.get("userId"); long myUserId = Long.parseLong(strUserId); try { this.user = this.userManager.getUserById(myUserId); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; } public String getPrePage() { return prePage; } public void setPrePage(String prePage) { this.prePage = prePage; } public String getJsonStr() { return jsonStr; } public void setJsonStr(String jsonStr) { this.jsonStr = jsonStr; } private boolean json2User() { boolean b = false; // json 反序列化 try { JSONObject js = JSONObject.fromObject(jsonStr); user = (User) JSONObject.toBean((JSONObject) js, User.class); b = true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return b; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public String getReturnStr() { return returnStr; } public void setReturnStr(String returnStr) { this.returnStr = returnStr; } public UserAction() { System.out.println("userAction create...."); } public UserManager getUserManager() { return userManager; } @Resource public void setUserManager(UserManager userManager) { this.userManager = userManager; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } /** * 如果已经登录,则转入到添加用户界面 否则,转入到登录界面 * * @return */ public String limit() { return SUCCESS; } /** * 添加用户信息 * * @return */ public String add() { user.getUserTypeNo().setNo(2); int result = userManager.add(user); if (result == 1) { return SUCCESS; } else { return ERROR; } } public String ajaxAdd() { // --------提示信息 json2User(); UserType userType = new UserType(); userType.setName("普通用户"); userType.setNo(2); user.setUserTypeNo(userType); int result = -1; try { result = userManager.add(user); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Map<String, String> myMap = new HashMap<String, String>(); if (result == 1) { // 添加用户成功 myMap.put("addUserFlag", "1"); } else { // 添加用户不成功! myMap.put("addUserFlag", "-1"); } JSONObject js = JSONObject.fromObject(myMap); this.returnStr = js.toString(); return SUCCESS; } // 验证用户名是否唯一 public String ajaxVUserAccount() { User u = null; try { u = this.userManager.getUserById(userId); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Map<String, String> myMap = new HashMap<String, String>(); if (u == null) { // 用户名验证通过 myMap.put("vUserAccountFlag", "1"); } else { // 用户名验证不通过 myMap.put("vUserAccountFlag", "-1"); } JSONObject js = JSONObject.fromObject(myMap); this.returnStr = js.toString(); return SUCCESS; } // 修改用户信息 public String ajaxUpdate() { // ----提示信息 json2User(); Map<String, String> myMap = new HashMap<String, String>(); int result = -1; try { result = this.userManager.update(user.getId(), user); result = 1; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } myMap.put("updateUserFlag", "" + result); JSONObject js = JSONObject.fromObject(myMap); this.returnStr = js.toString(); return SUCCESS; } // 根据用户账号删除用户 public String ajaxDelete() { Map<String, String> myMap = new HashMap<String, String>(); int result = -1; try { result = this.userManager.delete(userId); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } myMap.put("deleteUserFlag", "" + result); JSONObject js = JSONObject.fromObject(myMap); this.returnStr = js.toString(); return SUCCESS; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCounty() { return county; } public void setCounty(String county) { this.county = county; } public PlaceManager getPlaceManager() { return placeManager; } @Resource public void setPlaceManager(PlaceManager placeManager) { this.placeManager = placeManager; } /** * 界面上的地点与数据库的存储之间的转换 * * @return int 地点编号 */ public Place dealPlace() { Place place = null; if (province.equals(this.NOCHOOSE)) { // 没有选择 place = null; } else { if (city.equals(this.NOCHOOSE)) { // 选择了province place = this.placeManager.getProvince(province); } else { if (county.equals(this.NOCHOOSE)) { // 选择了province和city place = this.placeManager.getCity(province, city); } else { place = this.placeManager.getCounty(province, city, county); // 选择了province, city,county } } } return place; } /** * 对于用户的修改的处理 * * @return */ public String updatecl() { UserType userType = this.userManager.getUserTypeByName(user .getUserTypeNo().getName()); user.setUserTypeNo(userType); int result = this.userManager.updateByUserAccount(user.getAccount(), user); if (result == 1) { return SUCCESS; } else if (result == 0) { return "nouser"; } else { return ERROR; } } /** * 转入update显示页面 * * @return */ public String update() { try { this.user = this.userManager.getUserById(userId); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public PageResultSet<User> getPageResultSet() { return pageResultSet; } public void setPageResultSet(PageResultSet<User> pageResultSet) { this.pageResultSet = pageResultSet; } public String getKeyWord() { return keyWord; } public void setKeyWord(String keyWord) { this.keyWord = keyWord; } public String fenyeUser() { this.pageResultSet = this.userManager.queryByPage(5, this.currentPage); return SUCCESS; } // ajax分页查询 public String ajaxQuery() { try { this.pageResultSet = this.userManager.mhQuery(keyWord, pageSize, this.currentPage); System.out.println(this.pageResultSet); Map<String, Object> myMap = new HashMap<String, Object>(); myMap.put("currentPage", "" + this.pageResultSet.getPageInfo().getCurrentPage()); myMap.put("totalPage", "" + this.pageResultSet.getPageInfo().getTotalPage()); myMap.put("list", this.pageResultSet.getList()); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[] { "cars" }); JSONObject js = JSONObject.fromObject(myMap, jsonConfig); returnStr = js.toString(); // System.out.println("Info: userAction - ajaxFenyeQuery - returnStr:" // + returnStr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; } /** * 对于登录表单进行处理 * * @return */ public String loginFixed() { this.returnStr = ""; User u = this.userManager.getUserByAccount(user.getAccount()); if (u != null) { // 用户存在 user.setPwd(MD5.getMD5(user.getPwd().getBytes())); if (u.getPwd().equals(user.getPwd())) { if (u.getUserTypeNo().getName().equals("管理员")) { this.returnStr = "不是固定用户"; return "fail"; } else { session.put("userId", "" + u.getId()); session.put("userAccount", u.getAccount()); session.put("userTypeName", u.getUserTypeNo().getName()); prePage = (String) session.get("prePage"); session.remove("prePage"); if (prePage == null) { return "index"; } return SUCCESS; } } else { // 密码不正确 this.returnStr = "密码不正确"; return "fail"; } } else { // 用户不存在 this.returnStr = "用户不存在"; return "fail"; } } /** * 对于登录表单进行处理 * * @return */ public String loginAdmin() { this.returnStr = ""; User u = this.userManager.getUserByAccount(user.getAccount()); if (u != null) { // 用户存在 user.setPwd(MD5.getMD5(user.getPwd().getBytes())); if (u.getPwd().equals(user.getPwd())) { // System.out.println(">>loginCL>prePage:" + prePage); if (!u.getUserTypeNo().getName().equals("管理员")) { this.returnStr = "不是管理员用户"; return "fail"; } else { // 用户登录成功 session.put("userId", "" + u.getId()); session.put("userAccount", u.getAccount()); session.put("userTypeName", u.getUserTypeNo().getName()); prePage = (String) session.get("prePage"); session.remove("prePage"); if (prePage == null) { return "index"; } return SUCCESS; } } else { // 密码不正确 this.returnStr = "密码不正确"; return "fail"; } } else { // 用户不存在 this.returnStr = "用户不存在"; return "fail"; } } /** * 用户退出 * * @return */ public String logout() { try { session.clear(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; } /** * adjax方式用户退出 * * @return */ public String ajaxLogout() { Map<String, String> myMap = new HashMap<String, String>(); try { session.remove("userAccount"); session.remove("userTypeName"); myMap.put("logoutFlag", "1"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); myMap.put("logoutFlag", "-1"); } JSONObject js = JSONObject.fromObject(myMap); returnStr = js.toString(); System.out.println("userAccount-ajaxQuitUser-returnStr:" + returnStr); return SUCCESS; } public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this.session = session; } public long getUserId() { return userId; } public void setUserId(long userId) { this.userId = userId; } @Override public String toString() { return "UserAction [currentPage=" + currentPage + ", jsonStr=" + jsonStr + ", keyWord=" + keyWord + ", prePage=" + prePage + ", returnStr=" + returnStr + ", user=" + user + ", userId=" + userId + "]"; } public int getMyPageSize() { return myPageSize; } public void setMyPageSize(int myPageSize) { this.myPageSize = myPageSize; } }