我尝试呈现错误消息,但它没有出现:
我有验证器:
@Component
class ValidatorFBOSSearch implements Validator {
@SuppressWarnings("rawtypes")
@Override
public boolean supports(Class clazz) {
return FormBackObjectSearch.class.equals(clazz);
}
@Override
public void validate(Object obj, Errors error) {
FormBackObjectSearch fbos = (FormBackObjectSearch)obj;
if ("".equals(fbos.particularDate)){
error.rejectValue("particularDate", "required.listOfDates", "This field is required");
}
}
}
它必须检查我在表单支持对象中的字段是否输入了值。rejectValue 有三个参数。首先 - 在我的支持对象的模型属性中查找一个值,Secong 在属性文件中查找错误消息(我的属性文件位于资源/错误 forlder 中)methos 中的第三个参数表示它是否无法找到错误消息properties 文件将其呈现为默认值,这是我在 servlet-context.xml 中截取的代码
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
这是我的 messageSourse 的 xml 配置,用于在 servlet-context.xml 中获取 Validator 的错误:
<!-- Resolves error messages for validator from /Education/src/main/webapp/resources/errors-->
<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basename" value="errors/errormessages"/>
</beans:bean>
这里我的控制器代码和平处理请求:
@RequestMapping(value="/search", method=RequestMethod.GET)
public void search(Model model) {
FormBackObjectSearch fbos = new FormBackObjectSearch();
model.addAttribute("fbosAttribute", fbos);
}
@RequestMapping(value ="/result", method=RequestMethod.POST)
public String extract(@RequestParam String nameOfInstitution,
@RequestParam String particularDate,
@RequestParam String typeOfInstitution,
@ModelAttribute("fbosAttribute") FormBackObjectSearch fbos,
BindingResult result, RedirectAttributes redirectAttributes,
Model model) throws Exception {
ValidatorFBOSSearch validatorFBOS = new ValidatorFBOSSearch();
validatorFBOS.validate(fbos, result);
if(result.hasErrors()) {
redirectAttributes.addFlashAttribute("fbosAttribute", fbos);
return "redirect:/search";
} else {
if(particularDate !="" && nameOfInstitution !="" && typeOfInstitution =="") {
controllerSupportClass.findWithDateAndName(nameOfInstitution, particularDate, model);
} else if(particularDate !="" && nameOfInstitution =="" && typeOfInstitution !="") {
controllerSupportClass.findWithAddedDateAndType(typeOfInstitution, particularDate, model);
} else if(particularDate !="" && nameOfInstitution =="" && typeOfInstitution ==""){
controllerSupportClass.findWithAddedDate(particularDate, model);
} else if(particularDate !="" && nameOfInstitution !="" && typeOfInstitution !="") {
throw new Exception("Search by choose all parameters is not exceptable");
} else {
throw new Exception("You didn't put any search parameters");
}
}
return "search";
}
这是我的jsp的平静:
<form:form action="result" method="post" modelAttribute="fbosAttribute" >
<table>
<tr>
<th>Date for extracting:</th>
<td><form:select path="particularDate">
<form:option value=""> -Choose date-</form:option>
<form:options items="${listOfDates}"></form:options>
</form:select> <td><form:errors path="particularDate" cssClass="error"/></td>
</td>
</tr>
</table>
</form:form>
问题是我看不到错误消息出现。我尝试使用 Flash 属性在riderect 后出现错误,但没有任何问题。因为我在这里发现,当我使用重定向时,它会删除我的模型和错误并开始新的。但是我如何才能利用闪存属性来解决我的问题。谢谢