<form:select> Objekte Bindung durch Konverter in Spring MVC
Ich versuche zu Konverter zu organisieren Formular verbindlich in Spring MVC, wie es hier beschrieben ist: Feder form verbindlich, wie es zu tun ? Nicht konvertieren kann der Wert von type [java.lang.Zeichenfolge], um den gewünschten Typ, aber ich vermisse etwas, und die Bindung nicht funktioniert.
Einige Ausschnitte:
Personen:
public class Building {
private Long id;
private String address;
private BankAccount mainIncomeBankAccount;
//... getters, setters, hashCode() and equals()
}
public class BankAccount {
private Long id;
private String accountNumber;
//... getters, setters, hashCode() and equals()
}
JSP:
<form:form commandName="building" action="" method="post">
<form:input type="text" path="address"/>
<form:select path="mainIncomeBankAccount">
<form:option label="-- null value --" value="${null}"/>
<form:options items="${bankAccounts}" itemLabel="accountNumber" itemValue="id"/>
</form:select>
<input type="submit"/>
</form:form>
Controller:
@RequestMapping(value="/edit", method = RequestMethod.POST)
@PreAuthorize("hasRole('ROLE_PERMISSION')")
public String buildingAdd(@ModelAttribute("building") @Valid Building building,
//I already try not to use @ModelAttribute, @Valid, and both.
BindingResult result,
HttpServletRequest request,
Model uiModel,
RedirectAttributes redirectModel) {
//Some actions ...
}
Konverter:
@Component
public class BankAccountConverter implements Converter<String, BankAccount> {
@Inject
private BankAccountUtil bankAccountUtil;
public BankAccount convert(String id) {
return bankAccountUtil.findById(Long.getLong(id));
}
}
Servlet-Konfiguration:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<annotation-driven validator="validator"/>
<beans:bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<beans:property name="validationMessageSource" ref="messageSource"/>
</beans:bean>
<context:component-scan base-package="com.mvc.web" />
<beans:bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<beans:property name="converters">
<beans:list>
<beans:ref bean="bankAccountConverter"/>
</beans:list>
</beans:property>
</beans:bean>
Wenn ich das Formular abschicken building.mainIncomeBankAccount
Eigenschaft im controller ist immer nur null. Ich habe auch versucht zu verwenden, Formatter, aber ich habe das gleiche Ergebnis.
- Ihr Gebäude hat ein Feld wie dieses, Recht? Bankverbindung mainIncomeBankAccount; Auch haben Sie ein AnnotationMethodHandlerAdapter mit einem COnfigurableWebBindingInitializer auf die Sie gesetzt haben ConversionService?
Du musst angemeldet sein, um einen Kommentar abzugeben.
Müssen Sie diese Verbindung
<mvc:annotation-driven>
mit Ihrer conversion-service:<annotation-driven validator="validator" conversion-service="conversionService"/>
. Bekomme immer noch null-Ergebnis.Haben Sie versuchen, diese ohne die erste option:
Vielleicht bekommen Sie null, weil das ist, was Sie senden.
In Ihre JSP-Datei versuchen :