修改密码在什么地方做
做网站登录系统:
做了登录后可表示的设定页面,里面有修改密码功能,写修改密码时是在AccountService里写,还是在设定页面下面写。
请教(例如同样密码才可以修改。。。),请教
[解决办法]
写业务逻辑 在service或action中 写 都可以啊
/**
* 修改密码
* @return
*/
@Action(value="modifyPassword",results={@Result(name="modifyFail",location="/system/user/user-modifyPassword.jsp"),@Result(name="modifySuccess",location="/system/index.jsp")})
public String modifyPassword(){
String message = "";
try{
if(txtoldPasswd==null
[解决办法]
txtoldPasswd.equals("")){
message = "您输入的旧密码为空!";
throw new Exception(message);
}else if(txtnewPasswd==null
[解决办法]
txtnewPasswd.equals("")){
message = "您输入的新密码为空!";
throw new Exception(message);
}else if(txtrepeatpassword==null
[解决办法]
txtrepeatpassword.equals("")){
message = "您输入的重复密码为空!";
throw new Exception(message);
}else if(!txtnewPasswd.equals(txtrepeatpassword)){
message = "您输入的两次新密码不一致!";
throw new Exception(message);
}
}catch (Exception e) {
addActionMessage(message);
logger.error(message);
return "modifyPassword";
}
try {
//首先判断用户的旧密码是否正确
User user = (User)Struts2Utils.getSession().getAttribute("loginuser");
if(!user.getPassword().equals(txtoldPasswd)){
addActionMessage("抱歉,您输入的旧密码错误!");
return "modifyPassword";
}else{ //然后修改密码
user.setPassword(txtrepeatpassword);
securityEntityManager.saveUser(user);
Struts2Utils.getResponse().getWriter().print("<script>top.location.href='" + contextPvd.getAppCxtPath() +
"/login!goIndex.action'</script>");
return null;
}
} catch (Exception e) {
addActionMessage("密码修改失败!");
logger.error("密码修改失败!{}",e.getMessage());
}
return "modifyPassword";
}
public Navigation run() throws Exception {
Account ac = sessionScope("account");
System.out.println("Key:" + ac.getKey());
Account stored = service.get(ac.getId());
System.out.println("Key:" + stored.getKey());
Account ac2 = new Account();
BeanUtil.copy(request, ac2);
stored.setName(ac2.getName());
stored.setPassword(ac2.getPassword());
System.out.println("Paass:" + ac2.getPassword());
if (service.update(stored)) {
return forward("...");
} else {
return forward("...");
}
}
public boolean update(Account ac) {
// TODO Auto-generated method stub
if (ac.getName().trim().length() == 0)
return false;
...
...
...
Transaction tx = Datastore.beginTransaction();
Datastore.put(ac);
tx.commit();
return true;
}