如何获取 <c:forEach> 中某个值的值
How to get the value of a value in <c:forEach>
这是我的模特学生
public class Student {
List<Subject1> class1SubjectList=new ArrayList<Subject>();
List<Subject2> class2SubjectList=new ArrayList<Subject>();
List<Subject3> class3SubjectList=new ArrayList<Subject>();
List<Subject4> class4SubjectList=new ArrayList<Subject>();
List<Subject5> class5SubjectList=new ArrayList<Subject>();
List<Subject6> class6SubjectList=new ArrayList<Subject>();
List<Subject7> class7SubjectList=new ArrayList<Subject>();
List<Subject8> class8SubjectList=new ArrayList<Subject>();
List<Subject9> class9SubjectList=new ArrayList<Subject>();
}
................................................ ...................................
最多有 9 个 classes(No of classes)。它可以从 1 到 9.I 不等,想要渲染每个 class 中的所有主题${noOfClasses}
<c:forEach begin="1" end="${noOfClasses}" varStatus="theCount">
<select id="selectSunject${theCount.count}onClass">
<c:forEach items="${class${theCount.count}SubjectList}" var="subject">
<!-- Looking for class1SubjectList,class2SubjectList,class3SubjectList.. -->
<option value="${subject.id }" >${subject.name}</option>
</c:forEach>
</select>
</c:forEach>
嗨,
我知道上面的代码行不通。我试过了 this
<c:forEach begin="1" end="${noOfClasses}" varStatus="theCount">
<select id="selectSunject${theCount.count}onClass">
<c:set var="level" value="${theCount.count}"/>
<c:forEach items="${class[level]SubjectList}" var="subject">
<!-- Looking for class1SubjectList,class2SubjectList,class3SubjectList.. -->
<option value="${subject.id }" >${subject.name}</option>
</c:forEach>
</select>
</c:forEach>
正在显示...
${class[level]SubjectList}
contains invalid expression(s): javax.el.ELException: Failed to parse the expression ${class[level]SubjectList}
还是不行。有什么解决办法吗?提前致谢。
也许你可以设置一个列表列表。这是演示代码。
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:set var="class1SubjectList" value="${['a','b','c']}" />
<c:set var="class2SubjectList" value="${['d','e','f']}" />
<c:set var="class3SubjectList" value="${['g','h','i']}" />
<c:set var="listOfLists" value="${[class1SubjectList,class2SubjectList,class3SubjectList]}" />
<c:forEach var="currentList" items="${listOfLists}">
<c:forEach var="currentItem" items="${currentList}">
${currentItem}
</c:forEach>
</c:forEach>
打印 a b c d e f g h i。要了解语法,请查看 http://docs.oracle.com/javaee/7/tutorial/jsf-el004.htm
编辑:
如果您在 Student class 中创建一个列表列表并在您的 JSTL 中使用它会更好。但是,您可以使用反射来完成您想要完成的事情。不好的部分是您必须使用 scriptlet(实际上您可以将该代码移动到自定义 EL 函数或标记文件中)。这是演示代码。
package test;
import java.util.*;
public class Student {
public List<String> class1SubjectList = new ArrayList<String>();
public List<String> class2SubjectList = new ArrayList<String>();
public List<String> class3SubjectList = new ArrayList<String>();
public Student(){
class1SubjectList.add("a");
class1SubjectList.add("b");
class1SubjectList.add("c");
class2SubjectList.add("d");
class2SubjectList.add("e");
class2SubjectList.add("f");
class3SubjectList.add("g");
class3SubjectList.add("h");
class3SubjectList.add("i");
}
}
和 JSP
<%@ page import="test.Student, java.lang.reflect.Field, java.util.*" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
Student student = new Student();
Field[] fields = student.getClass().getFields();
List<List<String>> listOfLists = new ArrayList<List<String>>();
for(Field field: fields){
listOfLists.add((List)field.get(student));
}
pageContext.setAttribute("listOfLists", listOfLists);
%>
<c:forEach items="${listOfLists}" var="subjectList" varStatus="theCount">
<select id="selectSubject${theCount.count}onClass">
<c:forEach var="currentItem" items="${subjectList}">
<option value="${currentItem}" >${currentItem}</option>
</c:forEach>
</select>
</c:forEach>
这是我的模特学生
public class Student {
List<Subject1> class1SubjectList=new ArrayList<Subject>();
List<Subject2> class2SubjectList=new ArrayList<Subject>();
List<Subject3> class3SubjectList=new ArrayList<Subject>();
List<Subject4> class4SubjectList=new ArrayList<Subject>();
List<Subject5> class5SubjectList=new ArrayList<Subject>();
List<Subject6> class6SubjectList=new ArrayList<Subject>();
List<Subject7> class7SubjectList=new ArrayList<Subject>();
List<Subject8> class8SubjectList=new ArrayList<Subject>();
List<Subject9> class9SubjectList=new ArrayList<Subject>();
}
................................................ ...................................
最多有 9 个 classes(No of classes)。它可以从 1 到 9.I 不等,想要渲染每个 class 中的所有主题${noOfClasses}
<c:forEach begin="1" end="${noOfClasses}" varStatus="theCount">
<select id="selectSunject${theCount.count}onClass">
<c:forEach items="${class${theCount.count}SubjectList}" var="subject">
<!-- Looking for class1SubjectList,class2SubjectList,class3SubjectList.. -->
<option value="${subject.id }" >${subject.name}</option>
</c:forEach>
</select>
</c:forEach>
嗨,
我知道上面的代码行不通。我试过了 this
<c:forEach begin="1" end="${noOfClasses}" varStatus="theCount">
<select id="selectSunject${theCount.count}onClass">
<c:set var="level" value="${theCount.count}"/>
<c:forEach items="${class[level]SubjectList}" var="subject">
<!-- Looking for class1SubjectList,class2SubjectList,class3SubjectList.. -->
<option value="${subject.id }" >${subject.name}</option>
</c:forEach>
</select>
</c:forEach>
正在显示...
${class[level]SubjectList}
contains invalid expression(s): javax.el.ELException: Failed to parse the expression${class[level]SubjectList}
还是不行。有什么解决办法吗?提前致谢。
也许你可以设置一个列表列表。这是演示代码。
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:set var="class1SubjectList" value="${['a','b','c']}" />
<c:set var="class2SubjectList" value="${['d','e','f']}" />
<c:set var="class3SubjectList" value="${['g','h','i']}" />
<c:set var="listOfLists" value="${[class1SubjectList,class2SubjectList,class3SubjectList]}" />
<c:forEach var="currentList" items="${listOfLists}">
<c:forEach var="currentItem" items="${currentList}">
${currentItem}
</c:forEach>
</c:forEach>
打印 a b c d e f g h i。要了解语法,请查看 http://docs.oracle.com/javaee/7/tutorial/jsf-el004.htm
编辑:
如果您在 Student class 中创建一个列表列表并在您的 JSTL 中使用它会更好。但是,您可以使用反射来完成您想要完成的事情。不好的部分是您必须使用 scriptlet(实际上您可以将该代码移动到自定义 EL 函数或标记文件中)。这是演示代码。
package test;
import java.util.*;
public class Student {
public List<String> class1SubjectList = new ArrayList<String>();
public List<String> class2SubjectList = new ArrayList<String>();
public List<String> class3SubjectList = new ArrayList<String>();
public Student(){
class1SubjectList.add("a");
class1SubjectList.add("b");
class1SubjectList.add("c");
class2SubjectList.add("d");
class2SubjectList.add("e");
class2SubjectList.add("f");
class3SubjectList.add("g");
class3SubjectList.add("h");
class3SubjectList.add("i");
}
}
和 JSP
<%@ page import="test.Student, java.lang.reflect.Field, java.util.*" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
Student student = new Student();
Field[] fields = student.getClass().getFields();
List<List<String>> listOfLists = new ArrayList<List<String>>();
for(Field field: fields){
listOfLists.add((List)field.get(student));
}
pageContext.setAttribute("listOfLists", listOfLists);
%>
<c:forEach items="${listOfLists}" var="subjectList" varStatus="theCount">
<select id="selectSubject${theCount.count}onClass">
<c:forEach var="currentItem" items="${subjectList}">
<option value="${currentItem}" >${currentItem}</option>
</c:forEach>
</select>
</c:forEach>