标签:
public class StrutsRequestWrapper extends HttpServletRequestWrapper { public StrutsRequestWrapper(HttpServletRequest req) { super(req); } public Object getAttribute(String s) { ...... ActionContext ctx = ActionContext.getContext(); Object attribute = super.getAttribute(s);//先从request范围获取属性值 if (ctx != null) { if (attribute == null) {//如果从request范围没有找到属性值,即从ValueStack中查找对象的属性值 ...... ValueStack stack = ctx.getValueStack(); attribute = stack.findValue(s); ...... } } return attribute; } }
<s:set name="list" value="{‘zhangming‘,‘xiaoi‘,‘liming‘}" /> <s:iterator value="#list" id="n"> <s:property value="n"/><br> </s:iterator>
<s:set name="foobar" value="#{‘foo1‘:‘bar1‘, ‘foo2‘:‘bar2‘}" /> <s:iterator value="#foobar" > <s:property value="key"/>=<s:property value="value"/><br> </s:iterator
<s:if test="‘foo‘ in {‘foo‘,‘bar‘}"> 在 </s:if> <s:else> 不在 </s:else>
<s:if test="‘foo‘ not in {‘foo‘,‘bar‘}"> 不在 </s:if> <s:else> 在 </s:else>
<s:iterator value="books.{?#this.price > 35}"> <s:property value="title" /> - $<s:property value="price" /><br> </s:iterator>
public class BookAction extends ActionSupport { private List<Book> books; .... @Override public String execute() { books = new LinkedList<Book>(); books.add(new Book("A735619678", "spring", 67)); books.add(new Book("B435555322", "ejb3.0",15)); } }
示例代码:
book.java
package com.dzq.domain; public class Book { private int bookid; private String name; private int price; public int getBookid() { return bookid; } public void setBookid(int bookid) { this.bookid = bookid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public Book(int bookid, String name, int price) { this.bookid = bookid; this.name = name; this.price = price; } }
PersonListAction.java
package com.dzq.action; import java.util.*; import com.dzq.domain.Book; public class PersonListAction { private String name; private List<Book> books; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public String execute(){ books=new ArrayList<Book>(); books.add(new Book(1, "黑客与画家", 23)); books.add(new Book(2, "傲慢与偏见", 35)); books.add(new Book(3, "春娇与志明", 45)); name="小明"; return "list"; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="dzq" namespace="/dzq" extends="struts-default"> <action name="list" class="com.dzq.action.PersonListAction"> <result name="list">/WEB-INF/page/personlist.jsp</result> </action> </package> </struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setAttribute("user", "xiaoduc"); request.getSession().setAttribute("user", "hello"); %> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:property value="#request.user"/><br> <s:property value="#session.user"/><br> <s:set var="list" value="{‘第一个‘,‘第二个‘,‘第三个‘}" /> <s:iterator value="#list"> <s:property/><br> </s:iterator> <s:set var="maps" value="#{‘key1‘:90,‘key2‘:35,‘key3‘:40 }" /> <s:iterator value="#maps"> <s:property value="key" />= <s:property value="value"/><br> </s:iterator> <s:if test="‘foo‘ in {‘foo‘,‘value‘}"> 在 </s:if> <s:else> 不在 </s:else> </body> </html>
personlist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:property value="name"/> ${name }<br> <s:iterator value="books.{?#this.price>30}" > 书名:<s:property value="name"/><br> 价格:<s:property value="price"/><br> <hr> </s:iterator> </body> </html>
标签:
原文地址:http://www.cnblogs.com/xiaoduc-org/p/5453099.html