无法从< spring:hasBindErrors>获得任何输出.标签库 [英] Can't get any output from the <spring:hasBindErrors> taglib

查看:265
本文介绍了无法从< spring:hasBindErrors>获得任何输出.标签库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于Spring 3 MVC的Web应用程序,并将JSP用于我的视图层.我正在努力尝试报告JSP中特定Model对象的BindingResult错误的特定区域.最好用适当的代码对此进行解释:

I'm writing a Spring 3 MVC based web app, using JSPs for my view layer. I'm struggling on a particular area where I'm trying to report BindingResult errors for a particular Model object in a JSP. This is probably best explained with the appropriate code:

这是我的Spring Controller方法:

This is my Spring Controller method:

@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;
}

执行此方法时,我可以看到以下内容:

When I execute this method I can see the following:

  • 从HttpServletRequest正确创建了xApiRequest对象(我有一个单独的方法可以做到这一点)
  • 由@Valid注释引起的JSR-303验证已经发生,并确定了2个验证错误,正如我期望的那样,这些错误表示为BindingResult对象.
  • BindingResult对象存在于 Model 方法参数中.
  • xApiRequest和BindingResult对象已成功从 Model 方法参数传递到方法返回的 ModelAndView 对象.
  • The xApiRequest object is created correctly from the HttpServletRequest (I have a separate method which does this)
  • The JSR-303 validation, caused by the @Valid annotation, has taken place and has identified 2 validation errors, these are represented as BindingResult objects as I would expect.
  • The BindingResult objects are present in the Model method parameter.
  • The xApiRequest and BindingResult objects are successfully transferred from the Model method parameter in to the ModelAndView object which is returned from the method.

并且我可以确认BindingResult的内容确实显示为正确地将 xApiRequest 对象标识为验证错误的来源:

And I can confirm that the content of the BindingResult does appear to correctly identify the xApiRequest object as the source of the validation errors:

{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输出不包含异常包含错误详细信息的对象:

No matter what I do I can't seem to get the call to to recognise that the xApiRequest model object has binding errors, hence the JSP output does not contain the exceptions object containing details of the errors:

{
  w: false
  s: 200
  c: ""
  r: {
    o ~path: ""
  }
}

有人可以在这里看到我在做什么吗?失败了,有什么方法可以调试JSP处理期间发生的事情?我很想调试Spring taglib,但不确定如何在taglib和相关的代码段之间建立链接.

Can anyone see what I'm doing wrong here? Failing that is there any way I can debug what's going on during the JSP processing? I'm keen to debug the Spring taglib but am not quite sure how to make the link between the taglib and the associated bit of code.

希望我在这里提供了足够的信息,但是如果需要更多信息,请随时询问.

Hope I've provided enough information here but if any more is required then don't hesitate to ask.

非常感谢,

Edd

推荐答案

令人尴尬的是,在尝试调试 BindErrorsTag 类后,我意识到它根本没有被调用.这一发现使我意识到,我没有在JSP中包含Spring taglib命名空间声明,而这解决了这个问题.

How embarassing, after trying to debug the BindErrorsTag class I realised it wasn't being invoked at all. This discovery led me to realise that I hadn't included Spring taglib namespace declaration in the JSP, including this solved the problem.

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

使我自己错过了这个非常明显的错误,并对为什么JSP(和我的IDE)不抱怨缺少的taglib声明感到困惑.我以为缺少标签库声明通常会在执行标签时导致RuntimeException,但事实并非如此(我希望这样会为我节省很多调试时间!)

Cursing myself for missing this now very obvious error and somewhat confused as to why the JSP (and my IDE) didn't complain about the missing taglib declaration. I thought a missing taglib declaration usually caused a RuntimeException upon execution of the tag but it seems this is not so (I wish it was as it would have saved me a good couple of hours of debugging!)

反正问题解决了.

@axtavt-感谢您的帮助!

@axtavt - Thanks for the help!

这篇关于无法从&lt; spring:hasBindErrors&gt;获得任何输出.标签库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆