struts2 验证失败时重新填充控件

发布于 2024-09-12 02:33:58 字数 3285 浏览 3 评论 0原文

我是这个框架的新手。在我的页面中,我有一个下拉菜单和一个文本框。如果出现任何错误,则该掉落将不再可用。如何解决这个问题。

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>

页面:

<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>
                <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>

并通过CommentAction-validation.xml启用验证

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>
                <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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回旧景 2024-09-19 02:33:58

我能想到的有几个选项:

在会话中放置下拉列表对象

当您在 DropdownListAction 中填充下拉列表时,请在会话中放置对其的引用。这样,如果验证失败,您就可以在 JSP 上使用它。此解决方案的最后一步是在 CommentAction 成功执行后将其从会话中清除。

动作链

您可以使用链结果类型< /a> 作为您的输入结果。这将允许您在验证错误时自动调用 DropdownListAction。

<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>

问题是,这可能会扰乱您的输入字段的数量。你必须尝试一下。

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.

很酷又爱笑 2024-09-19 02:33:58

我找到了解决这个问题的最佳方法。

操作应实现 ValidationErrorAware 接口,该接口涉及覆盖 actionErrorOccurred 函数。

您只需粘贴所需的代码(例如下面示例中的 Dropdown 方法),然后返回 INPUT 而不是 SUCCESS

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文