Announcement Announcement Module
Collapse
No announcement yet.
Validation annotation with annotated controllers? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validation annotation with annotated controllers?

    Is it possible use a @Validation annotation (or implement a validate()) method to automatically invoke validation with annotated controllers?

    I have the following and I'd rather a more convention-based approach for invoking validation:

    Code:
      @RequestMapping(method = RequestMethod.POST)
      public String invite(ModelMap model, @ModelAttribute(COMMAND_NAME) IWEForm form, BindingResult result, HttpServletResponse response) throws Exception
      {
        log.info("in invite()...");
        new IWEFormValidator().validate(form, result);
        if (result.hasErrors())
        {
          log.info("validation failed: " + result.getFieldErrors());
          return execute(getRequest(), model);
        }
        else
        {
          model.addAttribute(COMMAND_NAME, form);
          return SUCCESS_VIEW;
        }
      }
    Thanks,

    Matt

  • #2
    Matt,

    I have poked around the code and docs, and don't see a way that this could be done with annotations either.

    Even in the sample petclinic app, the validators are used in exactly this way. I also poked around JIRA and didn't find an existing enhancement request like this (although I think it's a good idea too). Want to file a JIRA?

    Comment


    • #3
      Could you use a @Validation annotation when there are multiple @ModelAttribute annotated parameters? This almost seems to work except that when I change the form field and click submit, both bound objects are modified. And in the log file 9b looks like it's being overlaid by 9 in processSubmit.
      Code:
      package example.spring.web.controller;
      
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.stereotype.Controller;
      import org.springframework.validation.BindingResult;
      import org.springframework.web.bind.annotation.ModelAttribute;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.support.SessionStatus;
      import org.springframework.web.context.request.WebRequest;
      
      @Controller
      @RequestMapping("/post9.zug")
      public final class Post9Controller {
          private final transient Logger log = LoggerFactory.getLogger(getClass());
      
          private final static String viewName = "post9";
      
          @RequestMapping(method = RequestMethod.GET)
          public void getStub() {
              log.debug("called");
          }
      
          @ModelAttribute("boundObject9")
          public BoundObject9 formBindingObject() {
              log.debug("called");
      
              return (new BoundObject9("sample attr"));
          }
      
          @ModelAttribute("boundObject9b")
          public BoundObject9b formBindingObject9b() {
              log.debug("called");
      
              return (new BoundObject9b("sample attr 9b"));
          }
      
          @RequestMapping(method = RequestMethod.POST)
          public String processSubmit(@ModelAttribute("boundObject9")
          final BoundObject9 boundObject9, final BindingResult result9,
                  @ModelAttribute("boundObject9b")
                  final BoundObject9b boundObject9b, final BindingResult result9b,
                  final SessionStatus status, final WebRequest webRequest) {
              log.debug("boundObject9: {}", boundObject9);
      
              log.debug("result9: {}", result9);
      
              log.debug("boundObject9b: {}", boundObject9b);
      
              log.debug("result9b: {}", result9b);
      
              log.debug("status: {}", status);
      
              log.debug("webRequest: {}", webRequest);
      
              status.setComplete();
      
              return (viewName);
          }
      }
      Code:
      <?xml version="1.0" encoding="ISO-8859-1" ?>
      
      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          pageEncoding="ISO-8859-1"%>
          
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
      <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
      
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      <html xmlns="http://www.w3.org/1999/xhtml">
      
      <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
          <title>post9</title>
      </head>
      
      <body>
      
      <div>
      boundObject9: <c:out value="${boundObject9}" />
      </div>
      
      <div>
      boundObject9b: <c:out value="${boundObject9b}" />
      </div>
      
      <div>
      <form:form modelAttribute="boundObject9">
          <form:input path="attr1" />
          <input type="submit" value="go" />
      </form:form>
      </div>
      
      </body>
      
      </html>
      Code:
      DEBUG: [22:22:05] example.spring.web.controller.Post9Controller.formBindingObject: called
      DEBUG: [22:22:05] example.spring.web.controller.Post9Controller.formBindingObject9b: called
      DEBUG: [22:22:05] example.spring.web.controller.Post9Controller.getStub: called
      DEBUG: [22:22:25] example.spring.web.controller.Post9Controller.formBindingObject: called
      DEBUG: [22:22:25] example.spring.web.controller.Post9Controller.formBindingObject9b: called
      DEBUG: [22:22:25] example.spring.web.controller.Post9Controller.processSubmit: boundObject9: example.spring.web.controller.BoundObject9@12a66ea[attr1=sample attr]
      DEBUG: [22:22:25] example.spring.web.controller.Post9Controller.processSubmit: result9: org.springframework.validation.BeanPropertyBindingResult: 0 errors
      DEBUG: [22:22:25] example.spring.web.controller.Post9Controller.processSubmit: boundObject9b: example.spring.web.controller.BoundObject9b@cab854[attr1=sample attr]
      DEBUG: [22:22:25] example.spring.web.controller.Post9Controller.processSubmit: result9b: org.springframework.validation.BeanPropertyBindingResult: 0 errors
      DEBUG: [22:22:25] example.spring.web.controller.Post9Controller.processSubmit: status: org.springframework.web.bind.support.SimpleSessionStatus@1b6772a
      DEBUG: [22:22:25] example.spring.web.controller.Post9Controller.processSubmit: webRequest: ServletWebRequest: uri=/springexample/post9.zug;client=127.0.0.1;session=561ACFED059FAA7C365C036542557B40
      DEBUG: [22:23:51] example.spring.web.controller.Post9Controller.formBindingObject: called
      DEBUG: [22:23:51] example.spring.web.controller.Post9Controller.formBindingObject9b: called
      DEBUG: [22:23:51] example.spring.web.controller.Post9Controller.processSubmit: boundObject9: example.spring.web.controller.BoundObject9@33c658[attr1=sample]
      DEBUG: [22:23:51] example.spring.web.controller.Post9Controller.processSubmit: result9: org.springframework.validation.BeanPropertyBindingResult: 0 errors
      DEBUG: [22:23:51] example.spring.web.controller.Post9Controller.processSubmit: boundObject9b: example.spring.web.controller.BoundObject9b@122c9df[attr1=sample]
      DEBUG: [22:23:51] example.spring.web.controller.Post9Controller.processSubmit: result9b: org.springframework.validation.BeanPropertyBindingResult: 0 errors
      DEBUG: [22:23:51] example.spring.web.controller.Post9Controller.processSubmit: status: org.springframework.web.bind.support.SimpleSessionStatus@f3552f
      DEBUG: [22:23:51] example.spring.web.controller.Post9Controller.processSubmit: webRequest: ServletWebRequest: uri=/springexample/post9.zug;client=127.0.0.1;session=561ACFED059FAA7C365C036542557B40

      Comment

      Working...
      X