组合组件的问题

时间:2011-02-21 13:25:52

标签: java spring jsp spring-mvc

我有一个代表一个国家的班级

国家级

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

对象用户有一个字段国家

public class User {
...

    private Country country;

...
}

在我的控制器中:

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

在我的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>

我的讯息属性

messages_en.properties

EN=English
JP=Japan
DE=Germany 

messages_de.properties

EN=Englisch
JP=Japan
DE=Deutschland

如何编写一个显示该国语言权的组合?我可以使用类似<spring:message code="label.countryCode" /&gt; ??

的内容

1 个答案:

答案 0 :(得分:0)

如果您使用<form:options>标签,我认为您无法从属性文件中显示正确的语言。解决方法是自己创建<option>标记: -

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

请记住,如果您使用此方法,那么您没有使用countryDescription类中的Country属性,因为国家/地区描述直接来自属性文件。