用于JSTL jsp选项组标签的Spring 3 MVC功能

时间:2013-08-22 22:00:31

标签: java spring jsp jstl

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

解决!!

1 个答案:

答案 0 :(得分:1)

注意案例:servicemodule != serviceModule

<c:foreEach>循环也不正确:itemGroupvar都使用varStatus,而循环内部从不使用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">

难怪你迷失了自己。