出现错误时,BindingResult不会显示任何错误

时间:2013-01-25 01:31:06

标签: java spring model-view-controller spring-mvc spring-annotations

我正在为我的某个表单使用基于注释的验证,并且我无法使用我在JSP文件中使用的 form:errors标记来显示任何错误。当我尝试调试它时,我发现BindingResult也没有显示任何错误,但我将文本框留空以测试验证。非常感谢任何指针,为什么我无法验证它。

以下是我的控制器类

@Controller
public class RequestController {
@RequestMapping(value = "/requestform", method = RequestMethod.GET)
public ModelAndView request() {
return new ModelAndView("requestform", "command", new RequestForm());
}

@RequestMapping(value = "/showResponse", method = RequestMethod.POST)
public String showResponse(@ModelAttribute("berequest") @Valid RequestForm berequest, BindingResult errors,
    ModelMap model) {

if (errors.hasErrors()) {
    return "requestform";
} else {


StringBuilder url = new StringBuilder()
            .append("http://" + berequest.getMachineName() + ":" + berequest.getPortNumber()
                + "/service/Service/v1/get/").append("USERID=" + berequest.getUserId())
            .append(",")
            .append("debugflag=" + berequest.isDebugFlag()).append("/")
            .append(berequest.getAttributeNames());

    RestTemplate restTemplate = new RestTemplate();

    String result = restTemplate.getForObject(url.toString(), String.class);

    ObjectMapper mapper = new ObjectMapper();
    Object json = mapper.readValue(result, Object.class);
    String indented = mapper.defaultPrettyPrintingWriter().writeValueAsString(json);

    model.addAttribute("response", (indented));

    return "resultform";
    }
}

以下是我的模特课 -

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;

public class RequestForm {

@NotNull
@NotBlank
@NotEmpty
private String userId;

.....
}

以下是我的requestform.jsp文件

    <fieldset>
        <legend>User Details:</legend>
            <form:label path="userId" for="input-one" class="float"><strong>User Id:</strong></form:label><br />
            <form:input path="userId" class="inp-text" name="input-one-name" id="input-one" type="text" size="100" />
            <form:errors path="userId" cssClass="error"/><br />

    </fieldset>

下面是我的context.xml文件

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!--  Allow proxys -->
    <aop:aspectj-autoproxy />

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven>
            <mvc:message-converters>
              <!--  Support AJAX processing with progressive rendering. Overrides HttpOutputMessage with RaptorResponseWriter -->
            <beans:bean class="com.host.kernel.filter.RaptorJacksonHttpMessageConverter"/>                  
            </mvc:message-converters>


    <context:component-scan base-package="com.host.config" />
    <context:component-scan base-package="com.host.app.raptor.controller" />        

    <!-- Handles HTTP GET requests by efficiently serving up static resources 
         in the corresponding directory -->
    <resources mapping="/js/**" location="/js/" />
    <resources mapping="/css/**" location="/css/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
</beans:beans>

谁能告诉我我在这里做错了什么?

0 个答案:

没有答案