【发布时间】:2011-07-12 15:58:17
【问题描述】:
我正在编写一个基于 Spring 3 MVC 的 Web 应用程序,将 JSP 用于我的视图层。我在尝试报告 JSP 中特定模型对象的 BindingResult 错误的特定区域上苦苦挣扎。这可能最好用适当的代码来解释:
这是我的 Spring Controller 方法:
@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 正确创建的(我有一个单独的方法来执行此操作)
- 由 @Valid 注释引起的 JSR-303 验证已经发生,并发现了 2 个验证错误,正如我所料,这些错误表示为 BindingResult 对象。
- BindingResult 对象存在于 Model 方法参数中。
- xApiRequest 和 BindingResult 对象已成功从 Model 方法参数传递到该方法返回的 ModelAndView 对象中。
而且我可以确认 BindingResult 的内容确实正确地将 xApiRequest 对象识别为验证错误的来源:
{xApiRequest=com.stretchr.xapi.entity.request.XAPIRequest@1e28608, 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 模型对象有绑定错误,因此 JSP 输出不包含 异常 em> 包含错误详细信息的对象:
{
w: false
s: 200
c: ""
r: {
o ~path: ""
}
}
谁能看到我在这里做错了什么?如果失败了,有什么方法可以调试 JSP 处理过程中发生的事情?我热衷于调试 Spring taglib,但不太确定如何在 taglib 和相关代码位之间建立链接。
希望我在这里提供了足够的信息,但如果需要更多信息,请不要犹豫。
非常感谢,
艾德
【问题讨论】:
标签: jsp spring-mvc jsp-tags taglib