1.
<action name="save" class="SaveUserAction"><!-- name是页面上调用的名字 class是appconf里的material.xml某一个action的id --> <!-- <result name="success" type="redirect">${url}</result> --> <result name="success">/WEB-INF/jsp/interim.jsp</result> <result name="fail">/WEB-INF/jsp/register.jsp</result> <result name="error">/WEB-INF/jsp/register.jsp</result> </action>
2.注册一个新用户,关心3个service, 用户,购物车,积分!
通过邮箱(用户名)去db中查询查看有无使用过,使用该直接跳转到错误页面,
//省略setter/getter方法 public class SaveUserAction extends SelleckActionSupport { private static final long serialVersionUID = 1L; private UserService userService; private CartService cartService; private PointService pointService; private Integer id; private String username; private String password; private String name; private Timestamp createtime; private UserDTO user; private String url; public String execute() { Timestamp ts = new Timestamp(new java.util.Date().getTime()); user.setSource("selleckbio");//用户来源 user.setCreatetime(ts);//创建时间 String pwdTemp = user.getPassword(); user.setPassword(MD5Maker.getMD5String(pwdTemp)); user.setLogincount(0);//登录次数 user.setLastlogin(ts);//最近一次登录时间 user.setLevel("1"); StringBuffer hql = new StringBuffer("select count(userDTO.id) from UserDTO userDTO where userDTO.email='"); hql.append(user.getEmail().replace("'", "''")).append("'"); int recan = userService.counthql(hql.toString()); if(recan>0){ this.addActionMessage(rb.getString("EMAILUSED")); return ERROR; } id = userService.create(user); if (id != 0) { user.setId(id); ActionContext actionContext = ActionContext.getContext(); HttpServletRequest request=ServletActionContext.getRequest(); HttpSession httpSession = request.getSession(true); String sessionId = httpSession.getId(); Map session = actionContext.getSession(); user.setPassword(pwdTemp); user = userService.auth(user); cartService.getBackFromSession(user.getId(),sessionId); if(session.get(sessionId)!=null){ session.put(user.getId().toString(), (String)session.get(sessionId)); session.remove(sessionId); } session.put(Constants.SESSIONID,user); String url_Session = (String)ActionContext.getContext().getSession().get("urlRef"); if(url_Session!=null&&!url_Session.equals("")){ if(url_Session.indexOf("addCart.jhtml")!=-1 || url_Session.indexOf("checkout.jhtml")!=-1){ url_Session="/cart/checkout.jhtml"; //url_Session.replace("addCart", "checkout"); } url = url_Session.toString(); } String serverPort = (request.getServerPort() + "").equals("80") ? "" : (":" + request.getServerPort()); if(url!=null){ if(url.equals("")||url.equals("http://"+request.getServerName()+serverPort+"/login.html")){ url = "/welcome.html"; } } creatPoint(user); return SUCCESS; } else { return FAIL; } } /** * 注册成功 添加积分记录 50 * @param user */ private void creatPoint(UserDTO user) { PointDTO pointDTO = new PointDTO(); pointDTO.setAction("Register"); DateFormat formatDate = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); pointDTO.setAddDate(formatDate.format(new Date()));//日期是String类型 pointDTO.setDescription(""); pointDTO.setImageNo(""); pointDTO.setImageType(""); pointDTO.setOrderNo(""); pointDTO.setPoint(50); pointDTO.setUserDTO(user); pointService.create(pointDTO); } }