ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 在Spring MVC中通过Converter绑定的对象

2019-07-09 07:18:35  阅读:223  来源: 互联网

标签:java type-conversion data-binding spring-mvc spring


我尝试使用Converter在Spring MVC中组织表单绑定,就像它在这里描述的那样:Spring form binding how to do it ? Cannot convert value of type [java.lang.String] to required type,但我错过了一些东西,绑定不起作用.

一些片段:

实体:

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));
    }
}

Servlet配置:

<?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属性时始终为null.我也尝试使用Formatter,但我得到了相同的结果.

解决方法:

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

<annotation-driven validator="validator" conversionService = "conversionService" />

标签:java,type-conversion,data-binding,spring-mvc,spring
来源: https://codeday.me/bug/20190709/1410370.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有