`
hhr_michael
  • 浏览: 72572 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类
最新评论

struts2+hibernate3+spring2读书笔记8(OGNL)

阅读更多
                                       第9章 OGNL    

本章导读语
           OGNL是一种功能强大的EL表达式,它简化了对数据的访问,使得开发人员能够通过非常简单的表达式访问对象层。在struts2的OGNL中,数据放在以下几个地方:ValueStack、request、session、application的顺序进行遍历

一. OGNL的使用

1. 通过OGNL获取各种属性

(1) 编写Action类:OgnlAction.java

        
 package amigo.struts.ognl;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

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


/**
 * OGNL简单操作使用举例
 * */
public class OgnlAction extends ActionSupport implements ServletRequestAware,SessionAware,ServletContextAware{
				private static final long serialVersionUID=1L;
				private HttpServletRequest request;
				private Map<String,String> session;
				private ServletContext application;
				
				/**用户名*/
				private String username;
				/**密码*/
				private String password;
				public HttpServletRequest getRequest() {
					return request;
				}
				public void setServletRequest(HttpServletRequest request) {
					this.request = request;
				}
				public Map<String, String> getSession() {
					return session;
				}
				public void setSession(Map session) {
					this.session = session;
				}
				public ServletContext getApplication() {
					return application;
				}
				public void setServletContext(ServletContext application) {
					this.application = application;
				}
				public String getUsername() {
					return username;
				}
				public void setUsername(String username) {
					this.username = username;
				}
				public String getPassword() {
					return password;
				}
				public void setPassword(String password) {
					this.password = password;
				}
				public static long getSerialVersionUID() {
					return serialVersionUID;
				}
				
				/**
				 * 定义处理登录请求的execute方法
				 * */
				public String execute(){
					//Action属性设置
					this.setUsername("amigo");
					this.setPassword("1234");
					ActionContext ctx = ActionContext.getContext();
					ctx.put("address", "北京市海淀区");
					//request属性设置
					request.setAttribute("address", "request中属性的值");
					//session属性设置
					session.put("address", "session中属性的值");
					//application属性设置
					application.setAttribute("address", "application中属性的值");
					return this.SUCCESS;
				}
				
}

(2)编写jsp页面:ognl.jsp

 
<%@ 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=UTF-8">
<title>OGNL简单操作实例</title>
</head>
<body>
		访问Action实例的属性时,使用和不使用“#”都是可以的:<br/>
		<s:property value="username"/><br/>
		<s:property value="password"/><br/>
		<s:property value="address"/><br/><br/>
		
		访问parameter中的信息,使用#parameters.paramName或#parameters["paramname"]:<br/>
		<s:property value="#parameters.address"/><br/>
		<s:property value='#parameters["address"]'/><br/><br/>
		
		访问request中的信息,使用#request.paramName或#request["paramName"]:<br/>
		<s:property value="#request.address"/><br/>
		<s:property value='#request["address"]'/><br/><br/>
		
		访问session中的信息,使用#session.paramName或#session["paramName"]:<br/>
		<s:property value="#session.address"/><br/>
		<s:property value='#session["address"]'/><br/><br/>
		
		访问application中的信息,使用#application.paramName或#application["paramName"]:<br/>
		<s:property value="#application.address"/><br/>
		<s:property value='#application["address"]'/><br/><br/>
		
		使用#attr.paramName或#attr["paramName"]访问时,按request>session>application顺序访问属性<br/>
		<s:property value="#attr.address"/><br/>
		<s:property value='#attr["address"]'/><br/><br/>
</body>
</html>
(3)编写Struts2配置文件:struts.xml

 <!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<include file="struts-default.xml"/>
	<package name="tags" extends="struts-default">
		<action name="ognl" class="amigo.struts.ognl.OgnlAction">
		<result name="success">/ognl/ognl.jsp</result>
	
		</action>
	</package>
</struts>

2.OGNL中的集合操作


 <%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>OGNL的集合操作实例</title>
	</head>
	<body>
		List类型集合:
		<br/>(1)iterator标签:<br/>
		<s:iterator value="{'香蕉', '苹果', '橘子', '樱桃'}" id="fruitName"> 
			<s:property value="fruitName"/>&nbsp;&nbsp;&nbsp;&nbsp;
		</s:iterator>
		
		<br/>(2)select标签(生成的select的各option的value属性与显示的名称相同):<br/>
		<s:select label="userName" name="userName" list="{'阿蜜果', 'amigo', '阿蜜果2'}" value="'阿蜜果2'" />
		
		<br/><br/>Map类型集合:
		<br/>(1)iterator标签:<br/>
		<s:iterator value="#{'1' : '香蕉', '2' : '苹果', '3': '橘子', '4' : '樱桃'}"> 
			<s:property value="key"/>:<s:property value="value"/>&nbsp;&nbsp;&nbsp;&nbsp;
		</s:iterator>
		
		<br/>(2)select标签(生成的select的各option中的value值为各key-value对的key):<br/>
		<s:select label="userName" name="userName" 
				list="#{'1' : '阿蜜果', '2' : 'amigo', '3' : '阿蜜果2'}" 
				listKey="key" 
				listValue="value" value="3"/>
	</body>
</html>

二. EL表达式

(1)编写Action类:ElAction.java


package amigo.struts.el;

import java.util.Map;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * EL表达式使用举例.
 */
public class ElAction extends ActionSupport implements ServletRequestAware,
			SessionAware, ServletContextAware {
	private static final long serialVersionUID = 1L;
	
	private HttpServletRequest request;
	
	private Map<String,String> session;
	
	private ServletContext application;
	
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public void setServletContext(ServletContext application) {
		this.application=application;
	}

	public String execute() {
		// request属性设置
		request.setAttribute("address", "request中属性的值");
		
		// session属性设置
		session.put("address", "session中属性的值");
		
		// application属性设置
		application.setAttribute("address", "application中属性的值");
		return this.SUCCESS;
	}
}

(2)在web.xml中配置初始化参数encoding

<context-param>
		<param-name>encoding</param-name>
		<param-value>UTF-8</param-value>
</context-param>


      (3)编写jsp页面:el.jsp

   
 <%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
String address = "Page范围中的address变量";
pageContext.setAttribute("address", address);
%>
<html>
	<head>
		<title>el表达式使用实例</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<script language="javascript">
			var oneDay = 24*60*60*1000;
			var expDate = new Date();
			expDate.setTime(expDate.getTime() + oneDay);
			var cookieExpires = expDate.toGMTString();
			document.cookie="loginName=amigo;expires=" + cookieExpires;
		</script>
	</head>
	<body>
		四种范围中变量的获取:<br/>
		Page范围:${pageScope.address}<br/>
		Request范围:${requestScope.address}<br/>
		Session范围:${sessionScope.address}<br/>
		Application范围:${applicationScope.address}<br/>
		
	<br/>用户请求参数的获取:<br/>
		获取单个用户请求参数:${param.loginName}<br/>
		获取用户请求参数的一组值:${paramValues.address[0]}、${paramValues.address[1]}<br/>
		
	<br/>cookie中变量的获取:<br/>
		${cookie.loginName}
	
	<br/>header中值的获取:<br/>
		HTTP连接头部的host值: ${header["host"]}<br/>
		HTTP连接头部的accept值: ${header["accept"]}<br/>
		HTTP连接头部的user-agent值: ${header["user-agent"]}<br/>
		
	<br/>应用范围内的初始化参数:<br/>
		初始化参数encoding:${initParam.encoding}<br/>
	
	<br/>常用request信息:<br/>
		取得请求的参数字符串:${pageContext.request.queryString}<br/>
		取得请求的URL,但不包括请求之参数字符串:${pageContext.request.requestURL}<br/>
		服务的web application 的名称 :${pageContext.request.contextPath}<br/>
		取得HTTP 的方法(GET、POST):${pageContext.request.method}<br/>
		取得使用的协议(HTTP/1.1、HTTP/1.0):${pageContext.request.protocol}<br/>
		取得用户名称:${pageContext.request.remoteUser}<br/>
		取得用户的IP地址:${pageContext.request.remoteAddr}<br/>
		
	<br/>常用session信息:<br/>
		判断session是否为新的:${pageContext.session.new}<br/>
		取得sessionID:${pageContext.session.id}
	</body>
</html>


(4)配置struts.xml

 
 <action name="el" class="amigo.struts.el.ElAction">
		<result name="success">/el/el.jsp</result>
		</action>

三. Lambda表达式

         
 <%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Lambda表达式实例</title>
	</head>
	<body>
		-1小于0:<s:property value="#isLessThanZero =:[#this < 0 ? 'true' : 'false'], #isLessThanZero(-1)"/><br/>
		2小于0:<s:property value="#isLessThanZero =:[#this < 0 ? 'true' : 'false'], #isLessThanZero(2)"/>
	</body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics