开发者

repopulate controls when validation fails in struts2

开发者 https://www.devze.com 2023-01-09 02:54 出处:网络
I am new this framework. In my page i have one dropdown and one text box. If any error comes that drop is no longer available. How to fix this.

I am new this framework. In my page i have one dropdown and one text box. If any error comes that drop is no longer available. How to fix this.

Struts.xml:

 <action name="*DropDown"
            class="com.mmm.ehspreg2.web.action.DropdownListAction" method="{1}" />

<action name="addComment" method="add"
            class="com.mmm.ehspreg2.web.action.product.CommentAction">
            <result name="input" type="tiles">addComment</result>
            <result name="error" type="tiles">addComment</result>
            <result name="success" type="tiles">reloadList</result>
        </action>

Page:

<s:form action="addComment" method="POST">
    <s:action name="getDivisionsDropDown" id="actFetchDivisions" executeResult="true"></s:action>
    <s:action name="getPropretyTypesDropDown" id="actFetchPropretyTypes" executeResult="true" ></s:action>

<table cellspacing="0" cellpadding="3" width="100%" border="0">
                <tr>
                    <td class="error"><s:actionerror /><s:actionmessage /> <s:fielderror></s:fielderror></td>
                </tr>
                <tr>
                    <td class="bdyRecords"><s:label>
                        <s:text name="common.division" />
                    </s:label></td>
                    <td class="bdyRecords" style="width: 1px">:</td>
                    <td class="bdyRecords"><s:if
                        test="#actFetchDivisions.lstEntities.size()>0">
                        <s:select cssClass="drop" list="#actFetchDivisions.lstEntities"
                            cssStyle="width:200px" tooltip="divisionName"
                            id="select_division" listKey="id" name="comment.divisionId"
                            listValue="value"></s:select>
                    </s:if></td>
                </tr>
                <tr>
                    <td class="bdyRecords"><s:label>
                        <s:text name="common.propertytype" />
                    </s:label></td>
                    <td class="bdyRecords" style="width: 1px">:</td>
                    <td class="bdyRecords"><s:select cssClass="drop"
                        list="#actFetchPropretyTypes.lstEntities" cssStyle="width:200px"
                        tooltip="propertyTypeName" id="select_propertytype" listKey="id"
                        name="comment.propertyTypeId" listValue="value"></s:select></td>
                </tr>
           开发者_如何学C     <tr>
                    <td class="bdyRecords"><s:label>
                        <s:text name="common.comment" />
                    </s:label></td>
                    <td class="bdyRecords" style="width: 1px">:</td>
                    <td class="bdyRecords"><s:textfield name="comment.commentText"
                        key="common.comment" size="50" maxlength="60" cssClass="textbxadd" /></td>
                </tr>
            </table>

</s:form>

And validation enabled by CommentAction-validation.xml


You've got a few options I can think of:

Put dropdown List object in the session

When you populate your dropdown in DropdownListAction, put a reference to it in the session. That way, it will be available to you on your JSP if validation fails. One final step to this solution would be cleaning it out of the session once CommentAction has executed successfully.

Action Chaining

You could use the Chain Result type for your input result. This would allow you to automatically invoke your DropdownListAction on validation errors.

<action name="addComment" method="add" class="com.mmm.ehspreg2.web.action.product.CommentAction">
   <result name="input" type="chain">addCommentDropDown</result>
   <result name="error" type="tiles">addComment</result>
   <result name="success" type="tiles">reloadList</result>
</action>

The catch is, this may mess up the population of your input fields. You'd have to experiment with it.


I found the best way to resolve this.

Action should implement the ValidationErrorAware interface which involved overriding the actionErrorOccurred function.

You just paste the code needed, such as the Dropdown method in the example below, and return INPUT instead of SUCCESS.

0

精彩评论

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