struts2 验证失败时重新填充控件
我是这个框架的新手。在我的页面中,我有一个下拉菜单和一个文本框。如果出现任何错误,则该掉落将不再可用。如何解决这个问题。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到的有几个选项:
在会话中放置下拉列表对象
当您在
DropdownListAction
中填充下拉列表时,请在会话中放置对其的引用。这样,如果验证失败,您就可以在 JSP 上使用它。此解决方案的最后一步是在CommentAction
成功执行后将其从会话中清除。动作链
您可以使用链结果类型< /a> 作为您的
输入
结果。这将允许您在验证错误时自动调用 DropdownListAction。问题是,这可能会扰乱您的输入字段的数量。你必须尝试一下。
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 onceCommentAction
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.The catch is, this may mess up the population of your input fields. You'd have to experiment with it.
我找到了解决这个问题的最佳方法。
操作应实现 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.