我是 Spring 的新手,我想创建“选项组选择”,但我无法做到这一点。
我想要一个输出如下,但在 HTML 类型中说
<select name="..." value"...">
 <optgroup label="Category 1">
  <option ... />
  <option ... />
 </optgroup>
 <optgroup label="Category 2">
  <option ... />
  <option ... />
  </optgroup>
 </select>
 General
       movies
       hobbies        
 Games
      football
      basketball
 Images
      officePics
      familyPics
      PresntationPics
 RingTones
      pop
      classical
      jazz
jsp代码编辑:更正一个
   <form:select multiple="single" path="servicemodule" id="servicemodule">
     <form:option value="None" label="--Select--" />
      <c:forEach var="service" items="${servicemodule}">
       <optgroup label="${service.key}">
       <form:options items="${service.value}"/>        
       </optgroup>
      </c:forEach>        
    </form:select>
控制器代码:有 4 个主要类别,每个类别下可以有许多子类别。这些可以从 getServiceModuleList 方法中检索。但我不知道在哪里实现循环以将不同的子类别存储在各自的主类别下。
 @Autowired
         private ServiceModule servicemodule;
编辑:正确的@ModelAttribute
        @ModelAttribute("servicemodule")
    public Map<String,List<String>> populateService() {
        String[][] mainCategory = new String[7][2];
        mainCategory[0][0]= "General"; mainCategory[0][1]= "general1234";
        mainCategory[1][0]= "Games"; mainCategory[1][1]= "games1234";
        mainCategory[2][0]= "Images"; mainCategory[2][1]= "images1234";
        mainCategory[3][0]= "Ringtones"; mainCategory[3][1]= "ringtone1234";
        Map<String,List<String>> serviceModule= 
        new LinkedHashMap<String,List<String>>();
        List<String> subCategory=new ArrayList<String>();
        List<ServicesPojo> services=
        servicemodule.getServiceModuleList("1",mainCategory[0][1],"0");
        for(ServicesPojo serviceName: services)
        {
            subCategory.add(serviceName.getServiceName().trim());
        }
        serviceModule.put(scats[0][0],subService);
        return serviceModule;
}
编辑:得到了循环的答案
    for(int i=0;i<mainCategory.length;i=i+2){
    List<String> subCategory=new ArrayList<String>();
        List<ServicesPojo> services=
        servicemodule.getServiceModuleList("1",mainCategory[0][i],"0");
        for(ServicesPojo serviceName: services)
        {
            subCategory.add(serviceName.getServiceName().trim());
        }
        serviceModule.put(mainCategory[i][0],subCategory);
      }
模型这有一个主要错误,我是否应该只保留字符串或列表混淆!
已编辑:现在更正了一个
  private List<String> servicemodule;
  public List<String> getServicemodule() {
    return servicemodule;
      }
public void setServicemodule(List<String> servicemodule) {
    this.servicemodule = servicemodule;
     }
错误描述
  org.springframework.beans.NotReadablePropertyException: 
  Invalid property 'serviceModule' of bean class 
  [springx.practise.model.SiteModel]: Bean property 'serviceModule' 
  is not readable or has an invalid getter method: 
  Does the return type of the getter match the parameter type of the setter?
解决了!!