我是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?
解决了!!
最佳答案
注意案例:servicemodule != serviceModule
。<c:foreEach>
循环也不正确: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">
难怪你迷失了自己。