1

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

索引.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>

验证器用户.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>
4

0 回答 0