当前位置:编程学习 > JS >>

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 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,