java - JSTL jsp 选项组标签的 Spring 3 MVC 功能

标签 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>

Controller 代码: 有 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?

解决了!!

最佳答案

关注案例:servicemodule != serviceModule

<c:foreEach>循环也不正确:它使用 itemGroup两者都是varvarStatus ,和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">

难怪你迷失了自己。

关于java - JSTL jsp 选项组标签的 Spring 3 MVC 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18391543/

相关文章:

java - Hibernate:不能对 OFFSET 和 LIMIT 使用命名参数吗?

java - 如何在 Spring Boot 中将图像文件保存在 src/main/resources/images 中?

javascript - WebSocket - 无效的状态行javascript

java - 将 SQL native 查询转换为 Spring 数据 JPA

java - 使用 Jasper Report 导出为带有嵌入图像的单个 HTML

java - 具有两个线程的 IllegalMonitorStateException

java - 如何为 <form :select> from controller using properties file? 分配选项值

java - AngularJS 指令替代 jsp 标签库?

java - 更改图像布局方向

java - hibernate javax.persistence.RollbackException : Error while committing the transaction