5

我尝试在 Spring MVC 中使用 Converter 来组织表单绑定,就像这里描述的那样:Spring form binding how to do it ? 无法将类型 [java.lang.String] 的值转换为所需的类型,但我错过了一些东西,并且绑定不起作用。

一些片段:

实体:

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>

控制器:

@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 ...
}

转换器:

@Component
public class BankAccountConverter implements Converter<String, BankAccount> {
    @Inject
    private BankAccountUtil bankAccountUtil;

    public BankAccount convert(String id) {
        return bankAccountUtil.findById(Long.getLong(id));
    }
}

小服务程序配置:

<?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>

当我在控制器中提交表单building.mainIncomeBankAccount属性时,它总是为空。我也尝试使用 Formatter,但我得到了相同的结果。

4

3 回答 3

1

您需要链接<mvc:annotation-driven>到您的转换服务:

<annotation-driven validator="validator" conversionService = "conversionService" />
于 2013-04-10T11:01:00.823 回答
0

您是否在没有第一个选项的情况下尝试了此操作:

<form:option label="-- null value --" value="${null}"/>

也许你得到 null 因为那是你要发送的。

于 2013-06-18T12:06:48.873 回答
0

在您的 JSP 文件中尝试:

<form:form commandName="building" action="" method="post">
    <form:input type="text" path="address"/>
    <form:select path="mainIncomeBankAccount">
        <option label="-- null value --" selected disabled />
        <form:options items="${bankAccounts}" itemLabel="accountNumber" itemValue="id"/>
    </form:select>
    <input type="submit"/>
</form:form>
于 2017-03-27T19:43:51.480 回答