Java 用于JSTL jsp选项组标签的Spring3MVC功能

Java 用于JSTL jsp选项组标签的Spring3MVC功能,java,spring,jsp,jstl,Java,Spring,Jsp,Jstl,我是Spring的新手,我想创建“选项组选择”,但我无法做到这一点 我想输出如下,但在HTML类型说 <select name="..." value"..."> <optgroup label="Category 1"> <option ... /> <option ... /> </optgroup> <optgroup label="Category 2"> <option ... />

我是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
已编辑:更正@modeldattribute

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

解决了

关注您的案例:
servicemodule!=服务模块

循环也不正确:它将
itemGroup
用于
var
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">
private-ServiceModule-ServiceModule;
...
地图服务模块
...
私有列表服务模块;
...
...

难怪你迷路了。

看你的情况:
servicemodule!=服务模块

循环也不正确:它将
itemGroup
用于
var
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">
private-ServiceModule-ServiceModule;
...
地图服务模块
...
私有列表服务模块;
...
...

难怪你迷路了。

好的朋友!!还更正了我的jstl标记。。我得到了结果。。很抱歉,为我的同名变量道歉。。我正在学习MVC,并根据您的建议更改变量名称!!干杯。你能建议一下如何使用控制器内部的循环吗。。每次使用时,我都会获得ArrayIndexOutOfboundsException。。当它尝试所有的主要类别!发现循环中的愚蠢错误。。。我的问题现在已经用答案更正了!!感谢您发现了这个愚蠢的案例错误!!好样的伙计!!还更正了我的jstl标记。。我得到了结果。。很抱歉,为我的同名变量道歉。。我正在学习MVC,并根据您的建议更改变量名称!!干杯。你能建议一下如何使用控制器内部的循环吗。。每次使用时,我都会获得ArrayIndexOutOfboundsException。。当它尝试所有的主要类别!发现循环中的愚蠢错误。。。我的问题现在已经用答案更正了!!感谢您发现了这个愚蠢的案例错误!!
    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?
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">