1

我是 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?

解决了!!

4

1 回答 1

1

看你的情况:servicemodule != serviceModule

<c:foreEach>循环也不正确:它itemGroup同时使用 forvarvarStatus, 并且itemGroup从不在循环内使用。相反,serviceModule使用,但未在任何地方定义。

而且我很难理解您的代码,原因之一是您对非常不同的事物使用相同的名称,并且没有将 List 类型的属性复数。

private ServiceModule servicemodule;
...
Map<String,List<String>> serviceModule
...
private List<String> servicemodule;
...
<form:select multiple="single" path="serviceModule" id="serviceModule">
...
<c:forEach var="itemGroup" items="${servicesModule}" varStatus="itemGroup">

难怪你迷失了自己。

于 2013-08-22T22:07:43.253 回答