关于Struts显示错误信息的方法:
都知道Struts中表单验证错误信息需要编辑com.工程名.struts下的ApplicationResources.properties文件,但是我不知道怎么编辑。
比如,我在登录界面的验证表单中有以下代码:
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();

if(userid==null&&userid.trim().length()<1){
errors.add("UserIDIsNull", new ActionMessage("UserIDIsNull"));
}
if(password==null&&password.trim().length()<1){
errors.add("PasswordIsNull", new ActionMessage("PasswordIsNull"));
}

return errors;
}现在是不是要在前台写一个<html:errors>用来显示呢?
请给一个显示错误的示例好吗?谢谢。

解决方案 »

  1.   

    ActionErrors errors = new ActionErrors();
    if(xxxx){
     errors.add("error", new ActionError("alert_rand"));
    }
    return (errors);jsp用<html:errors>就可以显示.
    我以前的校验就用这个,缺点就是必须在validate()里调用。
    现在我改用更好的,ActionMessages errors = new ActionMessages();优点是在ActionBean里就可以用了。
      

  2.   

    ActionErrors actionErrors = new ActionErrors();
    if(****.equals("")){
    ActionError error=new ActionError("error.eventcodeRequest");
    actionErrors.add("eventcodeRequest",error);
    }ApplicationResources.properties文件中写:error.eventcodeRequest=<tr><td><strong>\u653b\u51fb\u4ee3\u53f7\u662f\u5fc5\u987b\u7684</strong></td></tr>红字是编译的过来的语言,否则计算机不认识
      

  3.   

    谢谢,我就是想问问怎么输入和编译啊,比如我在action中写的是if(userid==null&&userid.trim().length()<1){
    errors.add("UserIDIsNull", new ActionMessage("UserIDIsNull"));
    }
    那么我在ApplicationResources.properties文件中该怎么写呢?写成UserIDIsNull=用户名为空??然后怎么将中文编译一下?
      

  4.   

    汉字写在 temp.properties 文件中,再使用 JDK 中的 native2ascii 命令转一下就可以了。native2ascii -encoding gbk temp.properties ApplicationResources.propertiesencoding 根据你实际的编码调整一下。我一般都弄个 bat 批处理文件放在桌面上文件全写成绝对路径,编辑完 temp.properties 后点一下就可以了。
      

  5.   

    用log4j很简单,功能很强大,可以随意定义你出错误的格式。查一下相关资料。
      

  6.   

    com.工程名.struts下的ApplicationResources.properties 你也必须在struts-config.xml中指定 ,因为默认不是在这个路径