Spring form:select - 默认情况下,multiple不选择第一个选项

时间:2013-04-10 10:08:51

标签: java spring jsp

我一直致力于使用Spring表单taglib(http://www.springframework.org/tags/form)的Spring项目。

我正在使用一些多个选择框来指示一些选项(国家,工厂......)当我将整个列表传递给选择时 - 一切都很好:默认选择选择列表的第一个选项。但是,当用户来自特定国家/地区时,将过滤列表并仅显示其国家/地区。在这种情况下,默认情况下不会选择第一个元素。

JSP页面:
    

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:select path="countryValues" multiple="true" size="9" style="width:192px;" cssErrorClass="field-error">
    <form:options items="${command.countries}" itemValue="countryCode" itemLabel="correctDisplayString"/>                                       
</form:select>

Command.java     

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

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

Controller.java     

@RequestMapping(value = "/overview", method = RequestMethod.GET)
public String overview(HttpServletRequest request, Model model) {
    Attrs attrs = getAttrs(request);
    UserLocale.initUser(getUser(request));
    User user = UserLocale.getUser();
    List<FactoryMaster> factoryList = getFactoryList(attrs);
    List<CountryMaster> countryList = getCountryList(attrs);
    Command command = initCommand(attrs);
    model.addAttribute(command);
    if(user.hasRole(User.NORMAL)&& user.getCountryCode() != null){

        if(countries == null){
             countries=  getDaoBuilder().getDaoCountry().countryMap();               
         }

        String isoCode = countries.get(user
                .getCountryCode());
        List<CountryMaster> buffer = new ArrayList<CountryMaster>();
        for(CountryMaster i : countryList){
            if(isoCode.equalsIgnoreCase(i.getIsoCode())){
                buffer.add(i);                  
            }
        }
        System.out.println("List size: "+buffer.size());
        command.setCountries(buffer);       
    }
    else{
        command.getCountries().addAll(getCountryList(attrs));
    }

    command.getModels().addAll(getModelList(attrs));
    command.setBrands(getBrandList(attrs));
    return "/reporting/overview";
}
private List<CountryMaster> getCountryList(Attrs attrs) {
    List<CountryMaster> result = new ArrayList<CountryMaster>();
    CountryMaster ct = new CountryMaster(CountryMaster.ISO_COUNTRY_JOKER, 00);
    ct.setDescription("ALL");
    result.add(ct);
    result.addAll(attrs.countryList);
    return result;
}

在HTML页面上,我可以在其他列表中看到第一个元素具有属性selected="selected"。任何人都知道为什么当我操纵我的清单时不是这种情况?或者有人知道这个选定的属性分配的重要性是什么? (这是javascript,java属性,......?)

提前致谢!

1 个答案:

答案 0 :(得分:1)

原来可以设置列表框的值:这段代码使得它非常容易修复:     

public String overview(HttpServletRequest request, Model model) {
Attrs attrs = getAttrs(request);
UserLocale.initUser(getUser(request));
User user = UserLocale.getUser();
List<FactoryMaster> factoryList = getFactoryList(attrs);
List<CountryMaster> countryList = getCountryList(attrs);
Command command = initCommand(attrs);
model.addAttribute(command);
if(user.hasRole(User.NORMAL)&& user.getCountryCode() != null){

    if(countries == null){
         countries=  getDaoBuilder().getDaoCountry().countryMap();               
     }

    String isoCode = countries.get(user
            .getCountryCode());
    List<CountryMaster> buffer = new ArrayList<CountryMaster>();
    for(CountryMaster i : countryList){
        if(isoCode.equalsIgnoreCase(i.getIsoCode())){
            buffer.add(i);                  
        }
    }
    System.out.println("List size: "+buffer.size());
    command.setCountries(buffer); 

    // FIXED SELECTION OF ELEMENT
    command.setFactoryValues(new String[]{isoCode});
    // FIXED SELECTION OF ELEMENT   

}
else{
    command.getCountries().addAll(getCountryList(attrs));
}

    command.getModels().addAll(getModelList(attrs));
    command.setBrands(getBrandList(attrs));
    return "/reporting/overview";
}

这样,您可以使用代码设置列表框的值,并且在打开页面时 - 该值已经存在,默认情况下将其选中。