要求<html:errors>中的name属性中的值,不是<html:errors>所在JSP文件对应的actionform的,是其它的bean的感谢,,我不太明白这块,请教。

解决方案 »

  1.   

    formBean里面的代码如下:public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
            ActionErrors errors = new ActionErrors();
            if(username ==null || username.trim().equalsIgnoreCase("")){
                errors.add("loginform.username.null.error",new ActionMessage("loginform.username.null.error"));
            }
            if(password==null || password.trim().equalsIgnoreCase("")){
                errors.add("loginform.password.null.error",new ActionMessage("loginform.password.null.error"));
            }
            return errors;
        }页面上的出错提示代码如下:
    <table width="50%" border="0" align="center">
            <tr>
                <td width="27%"><bean:message key="loginform.username.input.dis"/></td>
                <td width="73%"><html:text property="username" /><html:errors property="loginform.username.null.error"/></td>
            </tr>
            <tr>
                <td><bean:message key="loginform.password.input.dis"/></td>
                <td><html:password property="password" /><html:errors property="loginform.password.null.error"/></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><html:reset><bean:message key="form.reset.dis"/></html:reset>
                    <html:submit><bean:message key="form.submit.dis"/></html:submit></td>
            </tr>
        </table>注意在formBean里面的new ActionMessage("loginform.username.null.error"),这里会自动加载struts properties文件里面的值,我的配置文件是struts_context_zh_CN.properties,内容如下:
    #login form
    loginform.username.input.dis=用户名:
    loginform.username.null.error=<font color="red">用户名不能为空!</font>
    loginform.password.input.dis=口&nbsp;&nbsp;令:
    loginform.password.null.error=<font color="red">口令不能为空!</font>这样,如果出错的时候,就会显示 <font color="red">用户名不能为空!</font> ,或者 <font color="red">口令不能为空!</font>
      

  2.   

    建议使用properties来配置页面显示信息,特别是对于多语种显示要求的系统特别方便。
      

  3.   

    感谢楼上的非常完整的代码,但不是我想要的,您的代码没有在html:errors中写上name的名字,,当然我要的不是只有一个property属性,我还想加入name这个属性,如果按您的方法加,name肯定是formBean的名字了,但我想要的代码洽洽相反,name是别的bean的名字,如果是当前formbean的名字,那么一个property属性就OK了,那么没有必要struts组织还要加上name的属性,我感觉name的属性不是html:errors对应的formbean的名字,还可以是其它的,所以我就想请教大家,我理解是不是正确的,感谢。。
      

  4.   

    我给你的和name和formbean都没有关系,至少我在使用的时候,基本没有考虑使用name这个属性。你仔细看看,是配置文件struts_context_zh_CN.properties中去配置所有出错信息,从而所有的bean和页面都可以共用,只需要配置文件中的property的名字即可,这样实现系统的国际化要求。和formbean的名字完全没有关系的。