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 setMaterial
补充:Web开发 , Jsp ,
- 更多JS疑问解答:
- 几个验证11位手机号码格式的js代码
- js把图片转换成 base64代码
- js把base代码转换成图片
- JS 将 base64编码的图片转化为图片文件
- js中的定时器
- js如何获得FCKeditor控件的值
- 用js限制投票的cookie .目前设置的为:<input type="" class="" onclick="'window.location...
- JS验证,这块“牛皮”反复修改都不能实现
- 在JS中使用DOM模型
- 如何用JS 获取本地文件夹的文件列表
- js中new 了两个Object数组。怎么样将数组内容合并,重复的内容?
- 求实现自动生成图片缩略图的JS代码
- JS脚本网页问题
- js,代码中"object"和"Object"区别?
- js+flash实现网页图片切换效果,出现边框,单击激活此控件。