求一个struts的Validator框架验证登录的帐号和密码

解决方案 »

  1.   

    用户名验证,密码类似
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
    <validators> <validator type="requiredstring">
    <param name="fieldName">username</param>
    <message>username should not be null</message>
    </validator>
    <validator type="stringlength">
    <param name="fieldName">username</param>
    <param name="minLength">6</param>
    <param name="maxLength">10</param>
    <message>
    username should be between ${minLength} and ${maxLength}
    </message>
    </validator>
    </validators>
      

  2.   

    登陆的action里
    public String login() throws Exception{
    Map session=(Map)ActionContext.getContext().getSession();
    String username=(String)session.get("username");
    String pwd=(String)session.get("password");
    String password=new MD5().getMD5ofStr(pwd);
    String result=userInfoService.login(username, password);
    if ("用户名错误"==result) {

    this.addActionError("您输入的用户名不存在");
    return INPUT;
    }
    else if("密码错误"==result){
    this.addActionError("密码有误,请重新输入");
    return INPUT;
    }
    else {
    session.put("userInfo", userInfoService.getUserInfoByUserName(username));
    return SUCCESS;
    }
    }
    @SuppressWarnings("unchecked")
    @Override
    public String execute() throws Exception {
    String username=userInfo.getUsername();
    String password=new MD5().getMD5ofStr(userInfo.getPassword());
    String result=userInfoService.login(username, password);

    if ("用户名错误"==result) {

    this.addActionError("您输入的用户名不存在");
    return INPUT;
    }
    else if("密码错误"==result){
    this.addActionError("密码有误,请重新输入");
    return INPUT;
    }
    else {
    if ("true".equals(this.getIsRemember())) {
    Cookie cookie=new Cookie("alumni.userInfo.user",username+"-"+userInfo.getPassword());
    cookie.setMaxAge(60*60*24*30);
    ServletActionContext.getResponse().addCookie(cookie);
    }
    Map session=(Map)ActionContext.getContext().getSession();
    session.put("userInfo", userInfoService.getUserInfoByUserName(username));
    return SUCCESS;
    }
    }
      

  3.   

    噢,要用Validator框架,没注意看。
      

  4.   

    不是这样的
    是有个struts-config.xml
    validator-rules.xml
    validation.xml还有需要 ApplicationResources.properties 里面配置错误的信息
    还有页面
    我只是很模糊的知道有这些东西但在里面老是配了没效果
      

  5.   

    就是在action同目录下生成一个对应xml文件,在xml文件中设置Validator校验。比如action叫AbcAction,xml文件就应该是AbcAction-validation.xml然后程序在运行的时候就会自动加载xml文件进行校验,校验方式前面我已经发过了。
      

  6.   

    http://blog.csdn.net/tcspace/archive/2007/11/20/1895009.aspx