You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
when combining <mvc:annotation-driven> and DataBinder.initdirectfieldaccess, conversion problems cause ConversionFailedException instead of being handled by the DataBinder [SPR-6953]
#11618
When you have the element mvc:annotation-driven/ in the bean definition file
and you use DataBinder.initDirectFieldAccess,
conversion problems are not handled by the DataBinder, but lead to a ConversionFaileException.
When you remove the element mvc:annotation-driven/, the DataBinder handles conversion problems OK.
Example command object:
package org.example.web;
public class CommandObject {
private int value;
public int getValue() {
return value;
}
}
This was an unfortunate oversight in 3.0: ConversionExceptions were properly detected for bean property access but not for direct field access. Fixed for 3.0.2 now.
This will be available in tonight's 3.0.2 snapshot. Feel free to give it an early try if you have the chance, and let me know whether it works for you.
Hans Desmet opened SPR-6953 and commented
When you have the element mvc:annotation-driven/ in the bean definition file
and you use DataBinder.initDirectFieldAccess,
conversion problems are not handled by the DataBinder, but lead to a ConversionFaileException.
When you remove the element mvc:annotation-driven/, the DataBinder handles conversion problems OK.
Example command object:
package org.example.web;
public class CommandObject {
private int value;
public int getValue() {
return value;
}
}
Example controller
package org.example.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class ExampleController {
@RequestMapping
(value="/example.htm",method=RequestMethod.GET)public String example(Model model) {
model.addAttribute(new CommandObject());
return "/WEB-INF/JSP/example.jsp";
}
@RequestMapping
(value="/example.htm",method=RequestMethod.POST)public String example(
@ModelAttribute
CommandObject commandObject, BindingResult bindingResult) {return "/WEB-INF/JSP/example.jsp";
}
@InitBinder
public void initBinder(DataBinder dataBinder) {
dataBinder.initDirectFieldAccess();
}
}
/WEB-INF/JSP/example.jsp
<?xml version="1.0" encoding="UTF-8"?>
<%
@page
contentType="text/html" pageEncoding="UTF-8" session="false"%><%
@taglib
prefix="form" uri="http://www.springframework.org/tags/form" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<form:form commandName="commandObject">
<div><form:input path="value"/> <form:errors path="value"/></div>
<div><input type="submit" value="OK"/></div>
</form:form>
</body>
</html>
Steps to see the problem
Type blabla in the textbox and submit,
You get a HTTP 500
The stack trace shows a ConversionFailedException
Affects: 3.0.1
Referenced from: commits 53b6e1c
The text was updated successfully, but these errors were encountered: