我遍历项目列表,如果元素的状态等于学生或老师,则需要显示特定的下拉列表。以下代码显示了所有字段,但未显示任何元素的下拉列表!
<s:iterator value="listOfPeople" status="element"> ..... <s:if test='%{elements[#element.index].Status.equalsIgnoreCase("Student") || elements[#element.index].Status.equalsIgnoreCase("Teacher")}'> <s:select name="elements[%{#element.index}].Status" id="elements[%{#element.index}].Status" label="Status" list="#{'Student':'Student','Teacher':'Teacher'}" headerKey = "-1" headerValue=" " value="%{Status}" /> </s:if>
在控制器中:
PeopleModel peopleModel = new PeopleModel(); listOfPeople = peopleModel.findPeople(); System.out.println("size is:" + listOfPeople.size()); //returns 8 return "listPeople";
我的模特:
List<People> people = new ArrayList(); final Session session = HibernateUtil.getSession(); try { final Transaction tx = session.beginTransaction(); try { Criteria criteria = session.createCriteria(People.class, "People") people.addAll((List<People>) criteria.list()); if (!tx.wasCommitted()) { tx.commit(); } if (session.isOpen()) { session.close(); } System.out.println("size is ::: " + people.size()); //returns 8 return people; } catch (Exception e) { tx.rollback(); e.printStackTrace(); } } finally { HibernateUtil.closeSession(); } return null; }
以下代码的结果是自爆
Status: ${Status} <br/>value : [<s:property value="elements[%{#element.index}].Status" />] Status : Principle value : [] Status : Student value : [] Status : Teacher value : [] Status : Teacher value : [] Status : Teacher value : [] Status : Teacher value : [] Status : Teacher value : [] Status : Teacher value : []
以上结果显示了第一个人的状态与其他人的状态分离,这就是为什么最后一个值[]没有显示任何状态的原因。
如果我取出状态:$ {Status},结果将是
value : [] value : [] value : [] value : [] value : [] value : [] value : [] value : []
放在%{}整个表达式周围,而不是放在其他标记的其他属性中间:
%{}
还要对字符串使用更合适的相等函数
<s:if test='%{elements[#element.index].status.equalsIgnoreCase("Student") || elements[#element.index].status.equalsIgnoreCase("Teacher")}'>
编辑 :伙计,你在做很多奇怪的事情;
${Status}
<s:property value="elements[%{#element.index}].Status" />
elements
foo
getFoo()
.foo
.Foo
Foo
然后,如果有private Object Status,将其private Object status;与getter和setter一起更改为,并在页面中使用:
private Object Status
private Object status;
<s:iterator value="listOfPeople" status="element"> <s:if test='%{listOfPeople[#element.index].status.equalsIgnoreCase("Student") || listOfPeople[#element.index].status.equalsIgnoreCase("Teacher")}'>
或使用var
<s:iterator value="listOfPeople" status="element" var="row"> <s:if test='%{row.status.equalsIgnoreCase("Student") || row.status.equalsIgnoreCase("Teacher")}'>
或简单地
<s:iterator value="listOfPeople" status="element"> <s:if test='%{status.equalsIgnoreCase("Student") || status.equalsIgnoreCase("Teacher")}'>
奇怪的代码导致奇怪的结果…然后直接使用它:)