2013-04-10 25 views
4

我嘗試使用轉換器來組織在Spring MVC的形式結合,喜歡這裏的描述:Spring form binding how to do it ? Cannot convert value of type [java.lang.String] to required type,但我錯過了什麼,並結合不起作用。<form:select>對象通過轉換器在Spring MVC結合

一些片段:

實體:

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屬性始終只是空。我也嘗試使用Formatter,但我得到了相同的結果。

+0

你的大廈對象確實有這樣的領域,對不對? BankAccount mainIncomeBankAccount;另外你有與您已設置ConversionService一個COnfigurableWebBindingInitializer的AnnotationMethodHandlerAdapter上? – 2013-06-18 12:53:59

回答

1

你需要與你的轉換服務鏈接<mvc:annotation-driven>

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

改變我<註解驅動/>行到'<註解驅動驗證=「驗證」轉換服務=「conversionService」 />'。仍然得到空結果。 – 2013-04-10 11:54:50

0

你試試這個沒有第一個選項:

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

也許你會得到空,因爲這是你要發送的內容。

+0

那是很久以前的事了,我放棄了與springforms ......現在我只使用標準的HTML輸入。在我的情況下,它更簡單,更靈活。但我記得在提交表單之前我改變了選擇選項,所以... – 2013-06-21 14:06:37

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>