我正在写一个Spring MVC 3基于Web的应用程序,使用JSP作为视图层。我在特定领域挣扎,我试图在JSP中报告特定Model对象的BindingResult错误。这可能是最有相应的代码解释:无法获得来自任何输出<春天:hasBindErrors>标签库
这是我的春天控制器方法:
@RequestMapping(value = "/**", method = RequestMethod.GET)
public ModelAndView get(@ModelAttribute("xApiRequest") @Valid final XAPIRequest xApiRequest,
final BindingResult xapiBindingResult,
final HttpServletResponse response,
Model model) throws EntityNotFoundException {
String viewName = "/WEB-INF/views/get-single-entity.jsp";
/*
* Create a MAV passing in the original Model object which contains:
* 1: The 'xApiRequest' @ModelAttribute object.
* 2: The BindingResult for the 'xApiRequest' object.
*/
final ModelAndView mav = new ModelAndView(viewName, model.asMap());
final XAPIResponse<Resource> xApiResponse = buildXAPIResponse(false, 200, xApiRequest, null);
response.setStatus(200);
mav.addObject("xApiResponse", xApiResponse);
return mav;
}
当我执行这种方法,我可以看到以下内容:
- 将创建xApiRequest对象正确地将来自HttpServletRequest(I具有这是否一个单独的方法)
- 的JSR-303的验证,所造成的@Valid注释,已经发生,并确定了2个验证错误,这些被repre像我所期望的那样感觉为BindingResult对象。
- 的BindingResult对象存在于模型方法参数。
- xApiRequest和BindingResult对象已成功从模型方法参数传输到ModelAndView对象,该对象从该方法返回。
而且我可以证实,BindingResult的内容确实出现了正确识别xApiRequest对象作为验证错误的来源:
{[email protected], org.springframework.validation.BindingResult.xApiRequest=org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'xApiRequest' on field 'userId': rejected value [null]; codes [NotEmpty.xApiRequest.userId,NotEmpty.userId,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [xApiRequest.userId,userId]; arguments []; default message [userId]]; default message [may not be empty]
Field error in object 'xApiRequest' on field 'projectId': rejected value [null]; codes [NotEmpty.xApiRequest.projectId,NotEmpty.projectId,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [xApiRequest.projectId,projectId]; arguments []; default message [projectId]]; default message [may not be empty]}
而JSP是这样的:
<%@ page contentType="application/json; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<%@ page session="false" %>
<spring:hasBindErrors name="xApiRequest">
</spring:hasBindErrors>
<c:if test="${errors}">
<json:object name="exceptions">
<json:property name="exceptionCount" value="${errors.errorCount}" />
<json:property name="globalExceptionCount" value="${errors.globalErrorCount}" />
<c:forEach var="error" items="${errors.allErrors}" varStatus="index">
<json:property name="${index}" value="${error.defaultMessage}" />
</c:forEach>
</json:object>
</c:if>
无论我做什么,我似乎都无法接到呼叫以认识到xApiRequest model ob JECT有绑定错误,因此JSP输出不包含例外错误的包含对象的细节:
{
w: false
s: 200
c: ""
r: {
o ~path: ""
}
}
有人能看到我在做什么错在这里?没有办法,我可以在JSP处理过程中调试发生了什么?我热衷于调试Spring taglib,但我不太清楚如何在taglib和相关代码之间建立链接。
希望我在这里已经提供了足够的信息,但如果有更多的要求,那么请不要犹豫,问。
非常感谢,
埃德
谢谢。其实我已经试过了 - 当我这样做的时候,JSP呈现JSON对象(好像在说_were_ BindErrors存在,但对'$ {errors.errorCount}'等的调用不会产生任何错误,就好像没有错误数据存在,任何进一步的想法? – 2011-03-10 12:11:57
可悲的是,它完全一样的事情,我已经确定标签类'org.springframework.web.servlet.tags.BindErrorsTag',所以我要看看我是否可以调试发生了什么 – 2011-03-10 12:44:48
@Edd:看起来很奇怪,类似的代码适合我。 – axtavt 2011-03-10 12:57:57