public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors actionErrors=new ActionErrors();

if(userName==null || userName.equals(""))
{
actionErrors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("用户名不能为空!"));
}
else if(userName.length()>15)
{
actionErrors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("你输入的用户名长度超出系统限制!"));
}

if(userPassword==null || userPassword.equals(""))
{
actionErrors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("密码不能 为空!"));
}
else if(userPassword.length()>15)
{
    actionErrors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("密码长度超长!"));
}

return actionErrors;
}
在struts-config.xml中加上:validate=“true”;为什么验证还是无效啊???各位前辈帮帮忙……

解决方案 »

  1.   

    ActionMessage中的参数要转码,在.properties中定义,然后在这里应用
      

  2.   

    struts 验证的标准做法是前台页面和后台都要验证。要不frombean里面弄个validate方法干什么!
    你先尝试在validate方法里面设置断电或者打印下参数。看代码是否执行到这了。
      

  3.   

    直接使用validate.xml验证很方便的啊。
    对于你这个问题,在Struts-config.xml的action中配置了validate=true,还要配置input,作为错误输出页面
      

  4.   


        <action
          attribute="logInForm"
          input="/LogIn.jsp"
          name="logInForm"
          path="/login"
          scope="request"
          type="LogInAction"
          validate="true" >
          <forward name="success" path="/success.jsp" />
          <forward name="fail" path="/fail.jsp" />
        </action>
    我已经有一个input了啊,这是登陆界面!再加一个就会提示错误了!
      

  5.   

    晕,这个转码你不知道啊?
    C:\JDK1.6\jdk1.6.0_03\bin\native2ascii.exe
    输入你的中文,回车,把转码复制到properties中,然后定义一个名字,这个名子在ActionMessage中就直接用就可以,注意修改properties要重启服务
    比如,ApplicationResources.properties中内容如下# Resources for parameter 'com.knight.struts.ApplicationResources'
    # Project Test
    login.needpwd=\u8bf7\u8f93\u5165\u5bc6\u7801
    在Form中
    new ActionMessage("login.needpwd")就可以了
      

  6.   

    难道你出错误之后不是返回LogIn.jsp画面吗?用validation.xml完全可以呀。
      

  7.   

    如:public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) {
    ActionErrors errors = super.validate(mapping, request);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    try {
    if (errors.size("startDate") == 0 && errors.size("endDate") == 0) {
    Date start = format.parse(startDate);
    Date end = format.parse(endDate);
    if (end.compareTo(start) <= 0) {
    ActionMessage msg = new ActionMessage("errors.startAndEnd");
    errors.add("startAndEnd", msg);
    }
    }
    } catch (ParseException e) {
    }
    return errors;
    }errors.startAndEnd在ApplicationResources.properties中定义
    errors.startAndEnd \u7ED3\u675F\u65E5\u671F\u5FC5\u987B\u5927\u4E8E\u5F00\u59CB\u65E5\u671F页面上
    <span style="color: red">
    <html:errors property="startAndEnd" />
    </span>
      

  8.   

    其实是的这些你都可以在validation.xml中配一下,使用struts的验证框架,只有涉及到业务数据的一下特殊的处理才需要将错误放入ActionErrors中返回