最近在项目中发现在jsp页面上遍历集合还有这种写法:
?
personlist为List<Person>,遍历时过滤掉name属性为“jack”的元素:
<s:iterator value="personList.{?!name.equals(\"jack\")}"> <s:property value="name"/>--- <s:property value="password"/> </s:iterator>
?persons为Set<Person>,遍历时只取第一个元素:
<s:iterator value="persons.{?1==1}[0]"> <s:property value="name"/>--- <s:property value="password"/><br/> </s:iterator>
?
?Person类为:
public class Person { private String name; private String password; public Person(String name,String password){ this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
?
?
?
?Action为:
public class TestAction extends ActionSupport { private List<Person> personList; private Set<Person> persons; public List<Person> getPersonList() { return personList; } public void setPersonList(List<Person> personList) { this.personList = personList; } public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons) { this.persons = persons; } @Override public String execute() throws Exception { personList = new ArrayList<Person>(); personList.add(new Person("jack","1111")); personList.add(new Person("jack1","2222")); personList.add(new Person("jack2","3333")); personList.add(new Person("jack3","4444")); personList.add(new Person("jack4","5555")); persons = new HashSet<Person>(); persons.add(new Person("jack","1111")); persons.add(new Person("jack1","2222")); persons.add(new Person("jack2","3333")); persons.add(new Person("jack3","4444")); persons.add(new Person("jack4","5555")); //下面两行代码是将集合放入栈上下文中 //ActionContext.getContext().put("personList", personList); //ActionContext.getContext().put("persons", persons); return Action.SUCCESS; } }
??