2012-10-13 18 views
12

Ülkeler (Liste) ve bir Ülke nesnesini tutan bir kullanıcı nesnesinin listesini içeren bir Modelim var. Kullanıcının ülkesini seçebileceği görüşüne sahibim.
Bu benim jsp sayfası snippet'idir: Bu benim Hesap modeliyay mvc formu: etiketi seç

<form:select path="user.country"> 
    <form:option value="-1">Select your country</form:option> 
    <form:options items="${account.countries}" itemLabel="name" itemValue="id" /> 
</form:select> 

geçerli:

public class Account { 

    private User user; 
    private List<Country> countries; 

    public User getUser() { 
     return user; 
    } 

    public void setUser(User user) { 
     this.user = user; 
    } 

    public List<Country> getCountries() { 
     return countries; 
    } 

    public void setCountries(List<Country> countries) { 
     this.countries = countries; 
    } 
} 

zaman jsp yükler (GET) formu: select görüntüler Geçerli kullanıcının seçilen öğe ülke. Sorun şu ki, bu özel durumu aldığım zaman:

Field error in object 'account' on field 'user.country': rejected value [90]; 
    codes [typeMismatch.account.user.country,typeMismatch.user.country,typeMismatch.country,typeMismatch.org.MyCompany.entities.Country,typeMismatch]; 
    arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [account.user.country,user.country]; 
    arguments []; default message [user.country]]; 
    default message [Failed to convert property value of type 'java.lang.String' to required type 'org.MyCompany.entities.Country' for property 'user.country'; 
    nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.MyCompany.entities.Country] for property 'country': no matching editors or conversion strategy found] 

Bunu nasıl aşabilirim?

cevap

7

Bir şekilde Spring'in String'u Country'a dönüştürmesini söylemelisiniz. - trick yaptı

@Component 
public class CountryEditor extends PropertyEditorSupport { 

    private @Autowired CountryService countryService; 

    // Converts a String to a Country (when submitting form) 
    @Override 
    public void setAsText(String text) { 
     Country c = this.countryService.findById(Long.valueOf(text)); 

     this.setValue(c); 
    } 

} 

ve

... 
public class MyController { 

    private @Autowired CountryEditor countryEditor; 

    @InitBinder 
    public void initBinder(WebDataBinder binder) { 
     binder.registerCustomEditor(Country.class, this.countryEditor); 
    } 

    ... 

} 
+0

Teşekkür: İşte bir örnek. Henüz anlamadığım bir şey var. Verileri yayınlarken özel bir dönüştürücüye ihtiyacım olsaydı, verileri alırken neden birine ihtiyacım yoktu? (sayfa yüklendiğinde, seçilen ülke, kullanıcının sahip olduğu aynı ülke nesnesiydi) –

+0

@MrT. Spring MVC 'select' formlarını akıllıca ele alır. Formunuz: select '' path = "user.country" '. Dolayısıyla, kullanıcı zaten 42 numaralı bir ülkeye sahipse, 42 numaralı seçenek etiketinin bir "selected =" selected "' özelliği olacaktır. Daha fazla bilgi için, [seçme etiketi ile ilgili belgelere bakın (buraya tıklayın)] (http://static.springsource.org/spring/docs/current/spring-framework-reference/html/view.html#view-jsp-formtaglib -selecttag). –

+0

Harika! Sadece mükemmel çalışan adam, bunun nasıl çalıştığını daha çok anlamak isterim. – Gemasoft