简体   繁体   English

春季形式:选择-多个默认情况下不选择第一个选项

[英]Spring form:select - multiple does not select first option by default

I have been working on a Spring project that makes use of the Spring form taglib ( http://www.springframework.org/tags/form ). 我一直在研究一个利用Spring表单taglib( http://www.springframework.org/tags/form )的Spring项目。

I am using some multiple select boxes to indicate some options (Country, factory,...) 我正在使用一些多个选择框来指示一些选项(国家,工厂,...)
When I pass an entire list to the select - all is well: the first option of the select list is selected by default. 当我将整个列表传递给select时-一切都很好:默认情况下,select列表的第一个选项已选中。 However, when a user is from a specific country, the list is filtered and only his country is shown. 但是,当用户来自特定国家/地区时,将过滤列表,仅显示其国家/地区。 In this case the first element is not selected by default. 在这种情况下,默认情况下不会选择第一个元素。

JSP page: 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 Command.java

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

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

Controller.java 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;
}

On the HTML page, I can see in other lists that the first element has the attribute selected="selected" . 在HTML页面上,我可以在其他列表中看到第一个元素的属性为selected="selected" Anybody have any idea why this is not the case when I manipulate my list? 有人知道我操作列表时为什么不是这种情况吗? Or does anyone know what is resposible for this selected attribute allocation? 还是有人知道此选定属性分配有什么用? (Is this javascript, java attribute,...?) (这是javascript,java属性...吗?)

Thanks in advance! 提前致谢!

Turns out the value of the listbox can be set: this piece of code made it quite an easy fix: 原来可以设置列表框的值:这段代码使其很容易解决:

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

This way, you set the value of the listbox using code, and when the page is opened - the value is already there, making it selected by default. 这样,您可以使用代码来设置列表框的值,并且在打开页面时-该值已经存在,默认情况下将其选中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM