OGNL的相关知识

5 篇文章 0 订阅

本文主要介绍了struts2的OGNL表达式的强大功能

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@taglib prefix="s"  uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
	<script>
		window.οnlοad=function(){
			var a='<s:property value="a"/>';
		}
	</script>
</head>
<body>
	<%
		request.setAttribute("p", "pppppp");
	 	pageContext.setAttribute("r", "rrrrrrrr");
	%>
	<s:property value="uname"/>
	<s:property value="user.addr.city"/>
	<hr/>
	actionContext的值:<s:property value="a"/>
	<hr/>
	request的值:<s:property value="#request.b"/>
	另一种写法1:	<s:property value="#request['b']"/>
	另一种写法2:	<s:property value="#request[#parameters.a[0]]"/>
	<hr/>
	session的值:<s:property value="#session.c"/>
	<hr/>
	appliaction的值:<s:property value="#application.d"/>
	<hr/>
	parameters的值:<s:property value="#parameters.a[0]"/>
	<hr/>
	attr的值:<s:property value="#attr.b"/>
					<s:property value="#attr.p"/>
					<s:property value="#attr.r"/>
	<hr>	
	运算:<s:property value="2+4"/>	
	赋值:<s:property value="#request.p=123"/>		
	赋值后:<s:property value="#request.p"/>	
	<hr>
	OGNL操作集合
	<s:set var="list" value="{'a','b','c'}" scope="request">
	</s:set>	
	list集合的值:<s:property value="#request.list.get(2)"/>
	<hr>
	判断:<s:property value="'a' in #request.list"/>
	判断:<s:property value="'a' not in #request.list"/>
	<hr>
	<s:set var="map" value="#{'name':'zxs','age':#request.list.get(2)}" scope="request"></s:set>
	map集合的值:<s:property value="#request.map.age"/>
	判断:<s:property value="'age' in #request.map.keys"/>
	判断:<s:property value="'c'      in  #request.map.values"/>
	<hr>
	遍历:	
	<s:iterator value="#request.list"  var="temp">
		<s:property value="#temp"/>
	</s:iterator>
	<hr>
	调用方法:
	获取PI的值:<s:property value="@@PI"/>	
	<s:debug></s:debug>
</body>
</html>

package com;

import java.util.Map;

import bean.Address;
import bean.User;

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

public class TestOgnlContext extends ActionSupport{
	private User user;
	private String uname;

	public String test1() throws Exception{
		setUname("zhangxiaosheng");
		User u=new User(1,"zxs","123456");
		u.setAddr(new Address("china","shanghai","changning"));
		setUser(u);
		ActionContext ac=ActionContext.getContext();
		ac.put("a", "actionContext的值");
		((Map)(ac.get("request"))).put("b","request的值");
		((Map)(ac.get("session"))).put("c","session的值");
		((Map)(ac.get("application"))).put("d","appliaction的值");
		
		return SUCCESS;
	}
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}

}

测试结果如图:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值