Uploaded image for project: 'Spring Data Commons'
  1. Spring Data Commons
  2. DATACMNS-683

Enabling Spring Data web support breaks @ModelAttribute binding in Spring MVC

    XMLWordPrintable

    Details

      Description

      Given following Java config class

      package be.vdab.web;
      
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.data.web.config.EnableSpringDataWebSupport;
      import org.springframework.web.servlet.config.annotation.EnableWebMvc;
      import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
      
      // enkele imports
      @Configuration
      @EnableWebMvc
      @EnableSpringDataWebSupport
      @ComponentScan
      public class CreateControllerBeans extends WebMvcConfigurerAdapter {
      }
      

      , following Controller class

      package be.vdab.web;
      
      import org.springframework.stereotype.Controller;
      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.servlet.ModelAndView;
      
      import be.vdab.entities.Person;
      
      @Controller
      @RequestMapping(value = "/")
      public class PersonController {
      	private static final String TOEVOEGEN_VIEW = "/WEB-INF/JSP/index.jsp";
      
      
      	@RequestMapping(method=RequestMethod.GET)
      	ModelAndView get() {
      		return new ModelAndView(TOEVOEGEN_VIEW).addObject(new Person());
      	}
      	
      	@RequestMapping(method = RequestMethod.POST)
      	String post(@ModelAttribute Person person) {
      	  if (person == null) {
      		  throw new IllegalArgumentException("person IS NULL");
      	  }
      	  return "redirect:/";
      	}
      
      
      
      }
      

      and following JSP

      <%@page contentType="text/html" pageEncoding="UTF-8" session="false"%>
      <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
      <!doctype html>
      <html lang="nl">
      <head>
      <title>Add person</title>
      </head>
      <body>
      <form:form action="" method="post" commandName="person">
      <form:label path="name">Name:</form:label>
      <form:input path="name" autofocus="true"/>
      <input type="submit">
      </form:form>
      </body>
      </html>
      

      the method post in PersonController throws the InvalidArgumentException because the person parameter is null.

      Observation 1:
      This worked up to and including spring-data-jpa 1.7.2.RELEASE

      Observation 2:
      The bug disappears when @EnableSpringDataWebSupport is put in comment in CreateControllerBeans.java

      Observation 3:
      The bug disappears when @ModelAttribute is put in comment in PersonController.java

      You can clone a project that shows the bug from
      https://github.com/desmethans/springDataJpaError.git

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              olivergierke Oliver Drotbohm
              Reporter:
              desmethans Hans Desmet
              Last updater:
              Oliver Drotbohm
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: