开发者

Problem with combo component

I have class representive a country

COUNTRY CLASS

public class Country {
 private String countryCode;
 private String countryDescription;

public void setCountryDescription(String countryDescription) {
        this.countryDescription = countryDescription;
    }

    public String getCountryDescription() {
        return countryDescription;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getCountryCode() {
        return countryCode;
    }
}

Object User have a field Country

public class User {
...

    private Country country;

...
}

In my controller:

public ModelAndView showUser() {
ModelAndView mav = new ModelAndView();
List<Country> list = new ArrayList();
list = dao.getCountryList(); // codes: 'DE', 'EN', 'JA' ...
mav.getModel.put("countries", list);
User u =-new User();
return mav;
}

In my JSP

<td><form:label path="country">
    <spring:message code="label.country" />
    </form:label></td>
<td><form:select path="country">
    <form:option value="0" label="..." />
    <form:options items="${countries}"  itemValue="countryCode" itemLabel="countryDescription" />
    </form:select></td>
<td><form:errors path="country" cssClass="error" /></td>

My message properties

messages_en.properties

EN=English
JP=Japan
DE=开发者_如何转开发Germany 

messages_de.properties

EN=Englisch
JP=Japan
DE=Deutschland

How to write a combo that showed the country the right to language? Can I use somthing like that <spring:message code="label.countryCode" />??


If you are using <form:options> tags, I don't think you are able to display the right language from the properties file. The workaround is to create the <option> tag yourself:-

<form:select path="country">
    <form:option value="0" label="..." />

    <c:forEach var="country" items="${countries}">
        <option value="${country.countryCode}"><spring:message code="${country.countryCode}" /></option>
    </c:forEach>
</form:select>

Keep in mind that if you use this approach, then you are not using the countryDescription property from the Country class because the country description comes directly from the properties file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜