开发者

Struts 2 redirect on validation failure and keep error message

I'm trying to display a validation error message after a redirect of the input field of an action. I'm trying to use the MessageStoreInterceptor, but without much success. Here is the struts.xml:

        <action name="list" class="myAction" method="list">
        <interceptor-ref name="store">
            <param name="operationMode">RETRIEVE</param>
        </interceptor-ref>
        <interceptor-ref name="myDefaultInterceptorStack" />
        <result name="success">/list.jsp</result>
        <result name="input">/list.jsp</result>
    </action>
    <action name="add" class="myAction" method="add">
        <interceptor-ref name="store">
            <param name="operationMode">STORE</param>
        </interceptor-ref>
        <interceptor-ref name="myDefaultInterceptorStack" />
        <result name="success" type="redirectAction">
            <param name="actionName">list</param>
        </result>
        <result name="input" type="redirectAction">
            <param name="actionName">list</param>
        </result>
    </action>

The list method of the action sets the values of the dropdown in list.jsp, so it has to be executed before displaying the jsp.

The validation is in MyAction-add-validation.xml:

    <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
       "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message key="name.required" />
        </field-validator>
    <开发者_如何学Go;/field>
</validators>

If I call list.action, list.jsp is displayed correctly populated. Then I submit the form calling add.action and since I left the name field emtpy, the validation fails. The MessageStoreInterceptor stores the field error, then the input redirect points to the list action, but the list action apparently forwards to the input forward without executing the list method first.

So if I place the tag in the list.jsp, I can see the error message from the validation, but the dropdown in list.jsp is not populated because the list method of list.action has not been executed.

Is there any way to achieve what I need? Any helps is more than welcome. Thanks


As described here MyAction needs to implement Preparable, so that when the validation fails, since the prepare() method is called before it, we still have our controls populated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜