无法从 标签库
问题内容:
我正在编写一个基于Spring 3
MVC的Web应用程序,并将JSP用于我的视图层。我正在努力尝试报告JSP中特定Model对象的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;
}
当我执行此方法时,可以看到以下内容:
- 从HttpServletRequest正确创建了xApiRequest对象(我有一个单独的方法可以执行此操作)
- 由@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输出不包含包含错误详细信息的 exceptions
对象:
{
w: false
s: 200
c: ""
r: {
o ~path: ""
}
}
有人可以在这里看到我在做什么错吗?失败了,有什么办法可以调试JSP处理期间发生的事情?我很想调试Spring
taglib,但不确定如何在taglib和相关的代码段之间建立链接。
希望我在这里提供了足够的信息,但是如果需要更多信息,请不要犹豫。
非常感谢
问题答案:
在尝试调试 BindErrorsTag 类之后, 令人 尴尬的是,我意识到它根本没有被调用。这一发现使我意识到,我没有在JSP中包括Spring
taglib名称空间声明,包括它解决了这个问题。
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
诅咒自己错过了这个非常明显的错误,并且对为什么JSP(和我的IDE)没有抱怨缺少的taglib声明感到困惑。我以为缺少标签库声明通常会在执行标签时导致RuntimeException,但事实并非如此(我希望这样会为我节省很多调试时间!)
无论如何,问题解决了。
@axtavt-感谢您的帮助!