如何打印两个不同表单的错误信息在同一页面上有相同的字段名称?

时间:2014-02-25 05:05:02

标签: java jsp struts-1 struts-validation

我的视图有两种形式(登录表单和注册表单)。 现在我使用验证插件进行验证。 两种形式都有不同的Action和Form Bean。但是每个表单bean都有相同的字段名,即userName&密码。 现在,当验证器生成错误消息时,错误将打印在两个表单上。 如何将验证器中的密钥从字段名称更改为其他错误消息。 任何其他建议和解决方案也受到欢迎。 提前完成。

的index.jsp

<%@taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<div align="right" style="padding-right: 5%;">
    <html:form action="/login" styleClass="form-inline">
        <bean:message key="label.login.username"/><html:text property="userName" name="LoginForm"/>
        <bean:message key="label.login.password"/><html:password property="password" name="LoginForm" />
        <html:submit value="Login" styleClass="btn btn-primary" />
    </html:form>
    <p class="text-error"><html:errors property="userName"/></p>
    <p class="text-error"><html:errors property="password"/></p>
    <p class="text-error"><html:errors property="invalidLogin"/><p>
</div>
<div style="padding-left: 5%;">
    <div>
        <b>Register</b>
        <html:form styleClass="form-horizontal" action="register">
            <div class="control-group">
                <label class="control-label">Name</label>
                <div class="controls">
                    <html:text property="name" name="UserForm" /><p class="text-error"><html:errors property="name"/></p>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">UserName</label>
                <div class="controls">
                    <html:text property="userName" name="UserForm" /><p class="text-error"><html:errors property="userName"/></p>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">Password</label>
                <div class="controls">
                    <html:password property="password" name="UserForm" /><p class="text-error"><html:errors property="password"/></p>
                </div>
            </div>
            <div class="control-group">
                <div class="controls">
                    <html:submit value="Register" styleClass="btn btn-primary"/>
                </div>
            </div>
        </html:form>
        <html:errors property="name"/>
        <p class="text-error"><html:errors property="registerFail"/></p>
        <p class="text-success"><html:messages id="null" property="registerSuccess"/></p>
    </div>
</div>

验证-user.xml

<formset>

    <form name="UserForm">

        <field property="name" depends="required,minlength">

           <msg name="required" key="error.msg.namerequired" />
           <msg name="minlength" key="error.msg.minlen" />

           <arg name="minlength" key="${var:minlength}" position="0" resource="false"/>

           <var>
                <var-name>minlength</var-name>
                <var-value>3</var-value>
            </var>
        </field>

        <field property="userName" depends="required,minlength">
             <msg name="required" key="error.msg.usernamerequired" />
             <msg name="minlength" key="error.msg.minlen" />

             <arg name="minlength" key="${var:minlength}" position="0" resource="false"/>

             <var>
                <var-name>minlength</var-name>
                <var-value>3</var-value>
            </var>
        </field>

        <field property="password" depends="required,minlength">
             <msg name="required" key="error.msg.passwordrequired" />
             <msg name="minlength" key="error.msg.minlen" />

             <arg name="minlength" key="${var:minlength}" position="0" resource="false"/>

             <var>
                <var-name>minlength</var-name>
                <var-value>3</var-value>
            </var>
         </field>
    </form>
</formset>
<formset>
    <form name="LoginForm">
        <field property="userName" depends="required" >
            <msg name="required" key="error.msg.usernamerequired"/>
        </field>
        <field property="password" depends="required">
            <msg name="required" key="error.msg.passwordrequired"/>
        </field>
    </form>
</formset>

的struts-config.xml

<form-beans>
    <form-bean name="UserForm" type="com.sumit.form.UserForm"/>
    <form-bean name="LoginForm" type="com.sumit.form.LoginForm"/>
</form-beans>

<action-mappings>        
    <action name="UserForm" input="/index.jsp" path="/register" scope="session" type="com.sumit.action.RegisterAction" validate="true">
        <forward name="success"  path="/index.jsp"/>
        <forward name="fail"  path="/index.jsp"/>
    </action>

    <action name="LoginForm" input="/index.jsp" path="/login" scope="session" type="com.sumit.action.LoginAction" validate="true">
        <forward name="success"  path="/home.jsp"/>
        <forward name="fail"  path="/index.jsp"/>
    </action>
</action-mappings>

0 个答案:

没有答案