`
caleb_520
  • 浏览: 247253 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Struts2 OGNL表达式之三

阅读更多
Struts 2的OGNL中“#”号用法

在Struts 2中,OGNL的“#”主要有三种用法:
(1)访问OGNL上下文中的parameters、request、session、application和attr对象。
(2)构造Map,如:#{key1:value1,key2:value2,key3:value3,......}
(3)取得集合中的子集,如:books.{?#this.price > 20}

第一种OGNL的“#”用法,访问OGNL上下文中的parameters、request、session、application和attr对象:

创建OgnlForSessionAction.java文件,其代码如下:

package com.gxa.edu.struts2.action.ch7;

import com.opensymphony.xwork2.ActionContext;

public class OgnlForSessionAction {

	public String execute() {
		ActionContext context = ActionContext.getContext();
		context.getSession().put("username", "张三");
		context.getSession().put("password", "123");
		return "success";
	}
}


在struts-ch7.xml文件中添加如下代码:

<action name="ognlForSessionAction" 
class = "com.gxa.edu.struts2.action.ch7.OgnlForSessionAction">
	<result name="success">/ch7/ognlForSession.jsp</result>
</action>


最后,创建ognlForSession.jsp文件,其代码如下:

<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'ognlForSession.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>
    Session存放用户的名称:<s:property value="#session.username"/><br>
    Session存放用户的密码:<s:property value="#session.password"/>
  </body>
</html>


测试结果如下图



第二种OGNL的“%”符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值。例如:例如,<s:url value=“xxx”/>标签。此标签中的value属性的值标是一个字符串类型。所以,可以采用Struts 2的OGNL中“%”号。

在原来的ongl.jsp文件中,添加如下代码:
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib prefix = "s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'ognl.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>
    用户名:<s:property value="person.username"/>
    密码:<s:property value="person.password"/>
    <p>OGNL"%"的用途</p>
    <br><s:url value="%{person.password}"></s:url>
    <br><s:url value="#person.password"/></body>
</html>


运行后的结果如下图:



第三种OGNL的$符号主要有两个方面的用途:(1)国际化资源文件中,引用OGNL表达式,例如国际化资源文件中的代码:reg.agerange=国际化资源信息:年龄必须在${min}同${max}之间。(2)在Struts 2框架的配置文件中引用OGNL表达式,例如下面的代码片断所示:

大家看下面的代码:
<?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>
	<!-- 如果要上传中文名称的文件,将"struts.i18n.encoding"的值设置为GBK -->
	<constant name="struts.i18n.encoding" value="GBK"></constant>
	
	<package name="filedownload" extends = "struts-default" namespace = "/ch4">
		<default-action-ref name="download"></default-action-ref>
		<action name="download" 
class = "com.gxa.edu.struts2.ch4.action.FileDownloadAction" method = "download">
			<param name="filePath">/upload/工作成果.doc</param>
			<param name="testParam">${testParam}</param>
			<result name = "success" type = "stream">
				<param name="contentType">application/x-download</param>
				<param name="bufferSize">4096</param>
				<param name="inputName">downLoad</param>
				<param name="contentDisposition">filename=${fileContentDisposition}</param>
			</result>
		</action>
	</package>
</struts>


上面的代码中有两个代码:

(1)<param name="testParam">${testParam}</param>
(2)<param name="contentDisposition">filename=${fileContentDisposition}</param>

这就是ognl表达式中“$”用法,其实Struts2中也利用了设置注入的思想,这个非常的方便的将Java代码和xml进行数据交互。
  • 大小: 27 KB
  • 大小: 25.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics