Struts2 OGNL的一个例子
下面直接给出源码:
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
上一篇: .Net魔法堂:log4net详解