使用默认的ObjectName时:
model.addAttribute(myObject);
BindingResult将错误结果绑定到相应的输入路径,其对象名称为myObject。
但是,如果我为我的对象使用这样的新名称:
model.addAttribute("newObjectName",myObject);
BindingResult仍会将其对象名称设置为myObject,而不是“newObjectName”,并且结果错误不会被绑定。
那么,有没有办法让BindingResult工作而不将模型对象名称更改为默认值?
我的控制器
@RequestMapping(value="unit/unitAdd.do", method=RequestMethod.POST)
public final String saveUnitAdd(@Valid PmiUnit pmiUnit,Model model,BindingResult bindingResult,HttpServletRequest httpServletRequest,
HttpSession session){
logger.info("saveAddUnit called!");
boolean exists = unitService.isAlreadyExists("pmi_units", "uni_unit_name_pk",pmiUnit.getUniUnitNamePk());
if (exists) {
//exists and without state DEL, return exists error
logger.info("Unit Name_PK exists");
if(!unitService.isExistsAndDeleted("pmi_units", "uni_unit_name_pk",pmiUnit.getUniUnitNamePk())){
System.out.println("exists and not deleted");
bindingResult.rejectValue("uniUnitNamePk", "pmi.validator.constraints.unique");}
//exists but with state DEL, update this row
else{
unitService.updateDeletedUnit(pmiUnit);
mainService.getDefaultPmiUnit(pmiUnit, model);
httpServletRequest.setAttribute("fromAddUnit", "true");
httpServletRequest.setAttribute("addResult", "true");
model.addAttribute("pmiUnit",pmiUnit);
return "accessControl1/unit/unitManage";
}
}
//if has errors, return the same page
if (bindingResult.hasErrors()) {
model.addAttribute("pmiUnit",pmiUnit);
return "accessControl1/unit/unitAdd";
}
JSP部分
<form:form method="POST" id="pmiAddUnit" name="pmiAddUnit"
modelAttribute="pmiUnit" enctype="multipart/form-data"
action="unitAdd.do">
...
<table>
<tr>
<td align="left"><form:input id="uniUnitNamePk" name="uniUnitNamePk" path="uniUnitNamePk" class="margin-right-ten" /></td>
<td align="left"><form:errors class="error" path="uniUnitNamePk" /></td>
</tr>
</table>
...
</form:form>
导入 ** .domain.PmiUnit;
在域包中,pmiUnit的类名是PmiUnit。
因此,如果我将modelAttribute名称更改为“pmiUnitObject”。(好吧,我只是喜欢这种方式,不喜欢具有不可变名称的固定默认方式。而且,如果添加多个对象,您应该可以使用你的.jsp中的${variableName.propertyName}
之类的bean
我修改了这样:
model.addAttribute("pmiUnitObject",pmiUnit);
这是调试期间的变量BindingResult:
..尴尬,无法发布图片......
无论如何,你会在bindingResult.objectName中看到它仍然是“pmiUnit”,并且它不会将错误结果绑定到jsp form:errors标记。
答案 0 :(得分:3)
您没有模型属性,恰好有@Valid
的任意方法参数。接下来,您在方法参数的排序方面也存在问题。 BindingResult
必须直接遵循它适用的参数,在您的情况下是PmiUnit
参数。另请参阅参考指南的this section。
public final String saveUnitAdd(@Valid PmiUnit pmiUnit,BindingResult bindingResult,,Model modelHttpServletRequest httpServletRequest, HttpSession session){ ... }
在@Valid
旁边添加@ModelAttribute
并移除model.addAttribute
来电。
public final String saveUnitAdd(@Valid @ModelAttribute PmiUnit pmiUnit,BindingResult bindingResult,Model model,HttpServletRequest httpServletRequest, HttpSession session){ ... }
如果您想给它另一个名称,请将其包含在@ModelAttribute
注释中。默认设置是采用方法参数的名称。
public final String saveUnitAdd(@Valid @ModelAttribute("your-name-here") PmiUnit pmiUnit,BindingResult bindingResult,Model model,HttpServletRequest httpServletRequest, HttpSession session){ ... }
我还建议删除HttpServletRequest
参数,只需添加要添加到模型中的属性即可。我可能会将方法改写为类似下面的内容(也不确定为什么你还有HttpSession
?)。
@RequestMapping(value="unit/unitAdd.do", method=RequestMethod.POST)
public final String saveUnitAdd(@Valid @ModelAttribute("pmiUnitObject) PmiUnit pmiUnit, BindingResult bindingResult, Model model, HttpSession session){
logger.info("saveAddUnit called!");
boolean exists = unitService.isAlreadyExists("pmi_units", "uni_unit_name_pk",pmiUnit.getUniUnitNamePk());
if (exists) {
//exists and without state DEL, return exists error
logger.info("Unit Name_PK exists");
if(!unitService.isExistsAndDeleted("pmi_units", "uni_unit_name_pk",pmiUnit.getUniUnitNamePk())) {
System.out.println("exists and not deleted");
bindingResult.rejectValue("uniUnitNamePk", "pmi.validator.constraints.unique");}
//exists but with state DEL, update this row
}
}
if (!bindingResult.hasErrors() ) {
unitService.updateDeletedUnit(pmiUnit);
mainService.getDefaultPmiUnit(pmiUnit, model);
model.addAttribute("fromAddUnit", "true");
model.addAttribute("addResult", "true");
return "accessControl1/unit/unitManage";
} else {
return "accessControl1/unit/unitAdd";
}
}