Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling Spring Data web support breaks @ModelAttribute binding in Spring MVC [DATACMNS-683] #1150

Closed
spring-projects-issues opened this issue Apr 13, 2015 · 4 comments
Assignees

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Apr 13, 2015

Hans Desmet opened DATACMNS-683 and commented

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


Affects: 1.10 GA (Fowler)

Issue Links:

  • DATACMNS-711 DomainClassConverter triggered unnecessarily
    ("is duplicated by")

Referenced from: commits 38dbfbf, 9977c61

Backported to: 1.10.1 (Fowler SR1)

0 votes, 6 watchers

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Apr 13, 2015

Oliver Drotbohm commented

Any chance you add a test case to the project, so that mvn clean test actually fails? Currently the project builds just fine

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Apr 13, 2015

Hans Desmet commented

The project does not demostrate the bug via a test.
To see the bug, the project must be deployed
and in the welcome page you must enter a name and submit the page.
Hope this helps

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Apr 13, 2015

Hans Desmet commented

I managed to add a unit test to the github project that shows the bug.
Hope this helps !

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Apr 13, 2015

Oliver Drotbohm commented

That helped an awful lot! :) The slightly rearranged Converter setup in Fowler introduced an a bit too lenient matching of Converters and to be honest I didn't expect Spring MVC to actually try and trigger converters if the instance created (a person in your case) actually matches the target type of the controller method.

Long story short, this is fixed in master and the bugfix branch. I could verify your example working with a snapshot build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.