目錄結構: 下面直接給出源碼: ognl.jsp: [html] <%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>OGNL示例</title> </head> <body> <!-- OGNL顯示request、response中的值 --> <h3 align="left">Session和Request值</h3> request.materialName: <s:property value="#request.materialName" /><br/> session.materialName: <s:property value="#session.materialName" /><br/> <!-- OGNL顯示條件表達式過濾的數據 --> <h3 align="left">根據條件顯示數據</h3> <p>價格小於50元的建材</p> <ul> <s:iterator value="materials.{?#this.mainbid <50}"> <li><s:property value="materialName"/> 建材價格是 <s:property value="mainbid"/>元!</li> </s:iterator> </ul> <p> "人造石台面" 的庫存數量是: <s:property value="materials.{?#this.materialName=='人造石台面'}.{mount}[0]"/></p> <!-- OGNL新建Map類型數據集合,顯示子元素值 --> <h3 align="left">Map數據顯示</h3> <s:set name="frank" value="#{'material':'歐龍無苯油漆(六度)', 'mount':'500'}" /> <p>供銷商frank手裡還有建材: <s:property value="#frank['material']" /></p> <p>庫存量為:<s:property value="#frank['mount']" /></p> </body> </html> OgnlExampleAction.java: [java] package action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import model.Material; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class OgnlExampleAction extends ActionSupport { //設置request、response參數和需要顯示的數據集合定義 private HttpServletRequest request; private HttpSession session; private List<Material> materials; public String execute() throws Exception { request = ServletActionContext.getRequest(); session=request.getSession(); //設置request、session存放值 request.setAttribute("materialName","人造石台面 From request"); session.setAttribute("materialName","歐龍無苯油漆(六度) From session"); //初始化數據集合,集合類型為List materials = new ArrayList<Material>(); materials .add(new Material("歐龍無苯油漆(六度)", 100, 2000)); materials .add(new Material("6*10mm門套線紅影木夾板飾面(單面)", 20, 2900)); materials .add(new Material("人造石台面", 56, 800)); return SUCCESS; } public HttpServletRequest getRequest() { return request; } public void setRequest(HttpServletRequest request) { this.request = request; } public HttpSession getSession() { return session; } public void setSession(HttpSession session) { this.session = session; } public List<Material> getMaterials() { return materials; } public void setMaterials(List<Material> materials) { this.materials = materials; } } Material.java: [java] package model; //材料對象 public class Material { //材料名 private String materialName; //材料價格 private int mainbid; //材料數量 private int mount; //構造初始化數據 public Material(String materialName, int mainbid, int mount) { super(); this.materialName = materialName; this.mainbid = mainbid; this.mount = mount; } public String getMaterialName() { return materialName; } public void setMaterialName(String materialName) { this.materialName = materialName; } public int getMainbid() { return mainbid; } public void setMainbid(int mainbid) { this.mainbid = mainbid; } public int getMount() { return mount; } public void setMount(int mount) { this.mount = mount; } } struts.xml: [html] <?xml version="1.0" encoding="gb2312"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.i18n.encoding" value="gb2312"/> <package name="Tag" extends="struts-default"> <!-- 創建Action --> <action name="ognl" class="action.OgnlExampleAction"> <result name="success">/jsp/ognl.jsp</result> </action> </package> </struts> 結果: Session和Request值 request.materialName: 人造石台面 From request session.materialName: 歐龍無苯油漆(六度) From session //"歐....."這個字串第一次不輸出,刷新頁面時才顯示 根據條件顯示數據 價格小於50元的建材 6*10mm門套線紅影木夾板飾面(單面) 建材價格是 20元! "人造石台面" 的庫存數量是: 800 Map數據顯示 供銷商frank手裡還有建材: 歐龍無苯油漆(六度) 庫存量為:500