vam*_*shi 2 validation model-view-controller spring portlet
我有一个几乎没有验证的表格.在新表单提交期间,如果验证失败,我可以看到这些错误消息.但是,在我有意地将字段更改为空白并且提交表单时编辑表单期间,Jsp页面上没有显示错误消息,但我可以将控制器中的错误数量设置为1.
<portlet:actionURL var="actionUrl">
<portlet:param name="action" value="editCommunity"/>
<portlet:param name="communityID" value="${community.id}"/>
</portlet:actionURL>
<liferay-ui:tabs names="Details" />
<form:form commandName="community" method="post" action="${actionUrl}">
<form:hidden path="id"/>
<div><form:errors cssClass="portlet-msg-error" path="*"/></div>
<table class="manager-detail">
<tr>
<th class="portlet-form-field-label">
<label for="community_label_name"><spring:message code="community.label.name"/></label>
<span class="manager-field-required">*</span>
</th>
<td><form:input id="community_label_name" cssClass="portlet-form-input-field" path="name" size="30" maxlength="80" /></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我的编辑控制器方法.....
渲染编辑表格
@RequestMapping(params = "action=editCommunity")
public String showEditCommunityForm(final RenderRequest request,
@RequestParam(value="communityID") Long id, final Model model)
throws CommunityNotFoundException {
final ThemeDisplay themeDisplay = (ThemeDisplay) request
.getAttribute(WebKeys.THEME_DISPLAY);
model.addAttribute("community", communityService.getCommunity(id));
return "communityEdit";
}
Run Code Online (Sandbox Code Playgroud)
编辑的表格已提交
@RequestMapping(params = "action=editCommunity")
public void submitEditCommunityForm(final ActionRequest request,
final ActionResponse response,
@ModelAttribute("community") Community community,
BindingResult result, Model model) throws SystemException, PortalException {
communityValidator.validate(community, result);
if (result.hasErrors()) {
System.out.println("validation errors size..."+result.getErrorCount());
//model.addAttribute("community", community);
response.setRenderParameter("action", "editCommunity");
response.setRenderParameter("communityID", String.valueOf(community
.getId()));
}
Run Code Online (Sandbox Code Playgroud)
}
它不是完整的代码,而是一个块
我尝试了几个方面,比如将http方法从post更改为POST,但没有任何作用.验证在表单创建期间完美有效,但在编辑期间无效.
我错过了什么吗?请给我一些建议.
干杯Vamshi
保留验证错误消息可能是一个真正的痛苦!我已经尝试了很多东西 - 从配置portlet容器的重定向行为到使用jsr303而不是spring验证.
我一直拥有并且成功实施的唯一解决方案真的很难看:
在代码中,这看起来像这样:
@ActionMapping
public void invite(@ModelAttribute MyFormBean myFormBean,
BindingResult result, Model model) {
// validate indata
myValidator.validate(myFormBean, result);
// Workaround to preserve Spring validation errors
if (result.hasErrors()) {
model.addAttribute("errors", result);
return;
}
...
}
@RequestMapping
public String showForm(@ModelAttribute("myFormBean") MyFormBean myFormBean,
Model model) {
...
// Workaround to get the errors form-validation from actionrequest
Errors errors = (Errors) model.asMap().get("errors");
if (errors != null) {
model.addAttribute(
"org.springframework.validation.BindingResult.myFormBean", errors);
}
return "myForm";
}
Run Code Online (Sandbox Code Playgroud)
存储在模型"org.springframework.validation.BindingResult.*"下的信息将在动作处理和渲染处理之间自动删除,并且通过在"错误"中明确保留它,信息将可供视图使用.
这是一个丑陋的解决方案,您必须了解实现真正有效的方式,这是反直觉的,如果没有正确评论,这个代码很容易被不熟悉问题的人删除,但它不是很多代码,它的工作原理.
| 归档时间: |
|
| 查看次数: |
3596 次 |
| 最近记录: |