Struts OGNL表达式实例

Person.java

package com.bjsxt.struts2.ognl;

import java.util.Date;

public class Person {
	private String name;
	private int age;
	private Date birthday;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	

}

 OgnlAction1.java

package com.bjsxt.struts2.ognl;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class OgnlAction1 extends ActionSupport {
	private static final long serialVersionUID = -1494290883433357310L;
	private List<Person> persons;
	private String msg;
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String execute() throws Exception {
		
		
		ActionContext ctx = ActionContext.getContext();  // 获得ActionContext实例,以便访问Servlet API
		
		ctx.getApplication().put("msg", "application信息");// 存入application
		
		ctx.getSession().put("msg", "seesion信息");// 保存session
		
		HttpServletRequest request = ServletActionContext.getRequest();// 保存request信息
		request.setAttribute("msg", "request信息");
		
		persons = new LinkedList<Person>();   // 为persons赋值
		Person person1 = new Person();
		person1.setName("pla1");
		person1.setAge(26);
		person1.setBirthday(new Date());
		persons.add(person1);

		Person person2 = new Person();
		person2.setName("pla2");
		person2.setAge(36);
		person2.setBirthday(new Date());
		persons.add(person2);

		Person person3 = new Person();
		person3.setName("pla3");
		person3.setAge(16);
		person3.setBirthday(new Date());
		persons.add(person3);

		return SUCCESS;

	}

	public List<Person> getPersons() {
		return persons;
	}

	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}
}

 showOGNL.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'showOGNL.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>      
  
    <h3>访问OGNL上下文和Action上下文</h3>  
    
  
    <!-- 使用OGNL访问属性值--> 
  
    <p>parameters: <s:property value="#parameters.msg" /></p>  
  
    <p>request.msg: <s:property value="#request.msg" /></p>  
  
    <p>session.msg: <s:property value="#session.msg" /></p>  
  
    <p>application.msg: <s:property value="#application.msg" /></p>  
  
    <p>attr.msg: <s:property value="#attr.msg" /></p>  
  
    <hr />  
    
    <h3>访问值栈中的action的普通属性和集合属性</h3>
    <s:property value="msg"/><br/>
    ${param.msg }<br/>
    
    <ul>
        <li>访问List:<s:property value="persons"/></li>
		<li>访问List中某个元素:<s:property value="persons[1]"/></li>
		<li>访问List中某个元素的属性:<s:property value="persons[1].name"/></li>
		<li>访问List中元素某个属性的集合:<s:property value="persons.{age}"/></li>
		<li>访问List中元素某个属性的集合中的特定值:<s:property value="persons.{age}[0]"/> | <s:property value="persons[0].age"/></li>
  </ul>
  <hr/>
    <h3>用于过滤和投影(projecting)集合</h3>  
  
    <p>年龄大于20</p>  
  
    <ul>  
  
    <!--  判断年龄-->  
  
        <s:iterator value="persons.{?#this.age>20}">  
  
            <li><s:property value="name" /> - 年龄:<s:property value="age" /></li>  
  
        </s:iterator>  
  
    </ul>  
  
    <p>姓名为pla1的年龄: <s:property value="persons.{?#this.name=='pla1'}.{age}[0]"/></p>  
  
    <hr />  
  
    <h3>构造Map</h3>  
  
    <s:set name="foobar" value="#{'foo1':'bar1', 'foo2':'bar2'}" />  
  
    <p>The value of key "foo1" is <s:property value="#foobar['foo1']" /></p>  
  	
  	<hr /> 
  	
  	<h4>%符号的用法</h4>  
  
    <s:set name="foobar" value="#{'foo1':'bar1', 'foo2':'bar2'}" />  
  
    <p>The value of key "foo1" is <s:property value="#foobar['foo1']" /></p>  
  
    <p>不使用%:<s:url value="#foobar['foo1']" /></p>  
  
    <p>使用%:<s:url value="%{#foobar['foo1']}" /></p>  
  
  	<hr />
   		<%  
            request.setAttribute("req", "request scope");  
            request.getSession().setAttribute("sess", "session scope");  
            request.getSession().getServletContext().setAttribute("app",  
                    "aplication scope");  
        %>  
   		1.通过ognl表达式获取 属性范围中的值  
        <br>  
        <s:property value="#request.req" />  
        <br />  
        <s:property value="#session.sess" />  
        <br />  
        <s:property value="#application.app" />  
        <br />  
        <hr>  
  
       2.通过ognl表达式创建list 集合 ,并且遍历出集合中的值  
        <br>  
        <s:set name="list" value="{'eeeee','ddddd','ccccc','bbbbb','aaaaa'}"></s:set>  
        <s:iterator value="#list" var="o">  
            <!--   ${o }<br/>  --><!-- 用EL表达式输出 -->
            
            <s:property />  
            <br />  
        </s:iterator>  
        <br />  
        <hr>  
  
       3.通过ognl表达式创建Map 集合 ,并且遍历出集合中的值  
        <br>  
        <s:set name="map"  
            value="#{'1':'eeeee','2':'ddddd','3':'ccccc','4':'bbbbb','5':'aaaaa'}"></s:set>  
        <s:iterator value="#map" var="o">  
            <!--      ${o.key }->${o.value }<br/>   -->  
            <!-- <s:property value="#o.key"/>-><s:property value="#o.value"/><br/>   -->  
            <s:property value="key" />-><s:property value="value" />  
            <br />  
        </s:iterator>  
        <br />  
        <hr>  
      4.通过ognl表达式 进行逻辑判断  
        <br>  
        <s:if test="'aa' in {'aaa','bbb'}">  
            aa 在 集合{'aaa','bbb'}中;  
        </s:if>  
        <s:else>  
            aa 不在 集合{'aaa','bbb'}中;  
        </s:else>  
        <br />  
        <s:if test="#request.req not in #list">  
            	不 在 集合list中;  
        </s:if>  
        <s:else>  
           	 在 集合list中;  
        </s:else>  
        <br />  
        <hr>  
        
       5.通过ognl表达式 的投影功能进行数据筛选  
        <br>  
        <s:set name="list1" value="{1,2,3,4,5}"></s:set>  
        <s:iterator value="#list1.{?#this>2}" var="o">  
            <!-- #list.{?#this>2}:在list1集合迭代的时候,从中筛选出当前迭代对象>2的集合进行显示 -->  
            ${o }<br />  
        </s:iterator>  
        <br />  
        <hr>  
       6.通过ognl表达式 访问某个类的静态方法和值  
        <br>  
        <s:property value="@java.lang.Math@floor(32.56)" />  
  
        <s:property value="@com.rao.struts2.action.OGNL1Action@aa" />  
        <br />  
        <br />  
        <hr>  
      7.ognl表达式 迭代标签 详细  
        <br>  
        <s:set name="list2"  
            value="{'aa','bb','cc','dd','ee','ff','gg','hh','ii','jj'}"></s:set>  
        <table border="1">  
            <tr>  
                <td>索引 </td>  
                <td>值</td>  
                <td>奇?</td>  
                <td> 偶?</td>  
                <td>首?</td>  
                <td> 尾?</td>  
                <td>当前迭代数量</td>  
            </tr>  
            <s:iterator value="#list2" var="o" status="s">  
                <tr bgcolor="<s:if test="#s.even">pink</s:if>">  
                    <td>  
                        <s:property value="#s.getIndex()" />  
                    </td>  
                    <td>  
                        <s:property />  
                    </td>  
                    <td>  
                        <s:if test="#s.odd">Y</s:if>  
                        <s:else>N</s:else>  
                    </td>  
                    <td>  
                        <s:if test="#s.even">Y</s:if>  
                        <s:else>N</s:else>  
                    </td>  
                    <td>  
                        <s:if test="#s.first">Y</s:if>  
                        <s:else>N</s:else>  
                    </td>  
                    <td>  
                        <s:if test="#s.isLast()">Y</s:if>  
                        <s:else>N</s:else>  
                    </td>  
                    <td>  
                    <s:property value="#s.getCount()"/>  
                </td>  
                </tr>  
            </s:iterator>  
        </table>  
        <br>  
        <hr>       
        
        
       8.ognl表达式:  if/else if/else 详细<br>  
        <% request.setAttribute("aa",0); %>  
        <s:if test="#request.aa>=0 && #request.aa<=4">  
            	在0-4之间;  
        </s:if>  
        <s:elseif test="#request.aa>=4 && #request.aa<=8">  
            	在4-8之间;  
        </s:elseif>  
        <s:else>  
           	 大于8;  
        </s:else>  
        <br>  
        <hr>  
    9.ognl表达式: url 详细<br>  
        <% request.setAttribute("aa","sss"); %>  
        <s:url action="testAction" namespace="/aa/bb">  
            <s:param name="aa" value="#request.aa"></s:param>  
            <s:param name="id">100</s:param>  
        </s:url>  
        <br/>  
        <s:set name="myurl" value="'http://www.baidu.com'"></s:set>  
        value以字符处理:   <s:url value="#myurl"></s:url><br>  
        value明确指定以ognl表达式处理:    <s:url value="%{#myurl}"></s:url>  
        <br>  
        <hr>  
    10.ognl表达式: checkboxlist 详细<br>  
        1> .list 生成;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>  
        name:checkboxlist的名字<br>  
        list:checkboxlist要显示的列表<br>  
        value:checkboxlist默认被选中的选项,checked=checked<br>  
        <s:checkboxlist name="checkbox1" list="{'上网','看书','爬山','游泳','唱歌'}" value="{'上网','看书'}" ></s:checkboxlist>  
        <br>  
       	 以上生成代码:<br>  
          
            <input type="checkbox" name="checkbox1" value="上网" id="checkbox1-1" checked="checked"/>  
            <label for="checkbox1-1" class="checkboxLabel">上网</label>  
            <input type="checkbox" name="checkbox1" value="看书" id="checkbox1-2" checked="checked"/>  
            <label for="checkbox1-2" class="checkboxLabel">看书</label>  
            <input type="checkbox" name="checkbox1" value="爬山" id="checkbox1-3"/>  
            <label for="checkbox1-3" class="checkboxLabel">爬山</label>  
            <input type="checkbox" name="checkbox1" value="游泳" id="checkbox1-4"/>  
            <label for="checkbox1-4" class="checkboxLabel">游泳</label>  
            <input type="checkbox" name="checkbox1" value="唱歌" id="checkbox1-5"/>  
            <label for="checkbox1-5" class="checkboxLabel">唱歌</label>" <br> 
        2> .Map 生成;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>  
        name:checkboxlist的名字<br>  
        list:checkboxlist要显示的列表<br>  
        listKey:checkbox 的value的值<br>  
        listValue:checkbox 的lablel(显示的值)<br>  
        value:checkboxlist默认被选中的选项,checked=checked<br>  
        <s:checkboxlist name="checkbox2" list="#{1:'上网',2:'看书',3:'爬山',4:'游泳',5:'唱歌'}" listKey="key" listValue="value" value="{1,2,5}" ></s:checkboxlist>  
        <br>  
                       以上生成代码:<br>  
            <input type="checkbox" name="checkbox2" value="1" id="checkbox2-1" checked="checked"/>  
            <label for="checkbox2-1" class="checkboxLabel">上网</label>  
            <input type="checkbox" name="checkbox2" value="2" id="checkbox2-2" checked="checked"/>  
            <label for="checkbox2-2" class="checkboxLabel">看书</label>  
            <input type="checkbox" name="checkbox2" value="3" id="checkbox2-3"/>  
            <label for="checkbox2-3" class="checkboxLabel">爬山</label>  
            <input type="checkbox" name="checkbox2" value="4" id="checkbox2-4"/>  
            <label for="checkbox2-4" class="checkboxLabel">游泳</label>  
            <input type="checkbox" name="checkbox2" value="5" id="checkbox2-5" checked="checked"/>  
            <label for="checkbox2-5" class="checkboxLabel">唱歌</label>  
        <hr>  
        <s:debug></s:debug>
</body>  

</html>

 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>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant><!--访问静态方法时需要-->
    
	<include file="/com/bjsxt/struts2/ognl/ognl1.xml"/>
    


</struts>

 ognl1.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="ognl" extends="struts-default">
      

        <action name="ognl1" class="com.bjsxt.struts2.ognl.OgnlAction1">
            <result>/showOGNL.jsp</result>
        </action>
      

    </package>
    
</struts>

 配置完成后在浏览器地址栏中输入:http://localhost:8080/Struts2_1900_OGNL/ognl1.action?msg=aa进行访问。浏览器显示结果如下

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值