2013-04-10 1 views
4

Converter를 사용하여 Spring MVC에서 양식 바인딩을 구성하려고하는데 여기에 설명 된 것처럼 : Spring form binding how to do it ? Cannot convert value of type [java.lang.String] to required type,하지만 뭔가 빠졌고 바인딩이 작동하지 않습니다.<form:select> Spring MVC에서 Converter를 통해 바인딩하는 객체들

일부 조각 :

엔티티 :

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

서블릿 구성 :

내가 컨트롤러 형태 building.mainIncomeBankAccount 속성을 제출

<?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> 
는 언제나 null입니다. 나 또한 Formatter를 사용하려고 시도했지만 동일한 결과를 얻었습니다.

+0

건물 객체는 바로이 같은 필드가 무엇입니까? BankAccount mainIncomeBankAccount; 또한 ConversionService를 설정 한 COnfigurableWebBindingInitializer가있는 AnnotationMethodHandlerAdapter가 있습니까? –

답변

1

당신은 전환 서비스와 <mvc:annotation-driven>를 연결해야합니다

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

my 줄을''로 변경하십시오. 여전히 null 결과를 얻습니다. –

0

이 첫 번째 옵션을 사용하지 않고이 작업을 시도해 봤어 : 그건 당신이 보내는 무엇 때문에

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

은 아마 당신이 null 얻을. 당신의 JSP 파일 시도에서

+0

오래 전이었고 springforms를 포기했습니다 ... 이제 표준 HTML 입력 만 사용합니다. 제 경우에는 더 간단하고 유연합니다. 하지만 양식을 제출하기 전에 선택 옵션을 변경 한 것을 기억합니다 ... –

0

:

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