您的位置:首页 > 其它

客户关系管理系统9(注册用户功能,使用自定义异常在注册页面显示)

2018-01-28 20:12 302 查看

UserAction

/*
* 用户注册功能
*/
public String regist(){
try {
userService.saveUser(user);
} catch (Exception e) {
e.printStackTrace();
ActionContext.getContext().put("error",e.getMessage());
return "registError";
}
return "toLogin";
}


UserServiceImpl

public void saveUser(User u) {
//1.调用dao根据注册的登录名获得用户对象
User existU=ud.getByUserCode(u.getUser_code());
if(existU!=null){
//2.如果获得到user对象,用户名已经存在,抛出异常
throw new RuntimeException("用户名已经存在!");
}
//使用MD5对密码进行加密
u.setUser_password(MD5Utils.md5(u.getUser_password()));
//3.执行dao执行保存
ud.save(u);
}


小技巧:如何在页面显示自定义异常?

①首先在service层书写异常判断

if(existU!=null){
//2.如果获得到user对象,用户名已经存在,抛出异常
throw new RuntimeException("用户名已经存在!");
}


②在WEB层应用try。。catch。。进行异常捕捉。并将其放入名为error的域中,如果出现错误,将会跳转到名为registError的结果集中,然后在结果集中配置要跳转的页面

try {
userService.saveUser(user);
} catch (Exception e) {
e.printStackTrace();
ActionContext.getContext().put("error",e.getMessage());
return "registError";
}


③在页面上显示自定义异常信息

<font color="red" ><s:property value="#error" /> </font>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐