OGNL表达式语言(#号的用法)
用法1:访问OGNL上下文和Action上下文,#相当ActionContext.getContext()
jsp页面
配置struts_ognl.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="ognl" namespace="/ognl" extends="struts-default">
<action name="*_test" class="cn.itcast.ognl.{1}" method="test">
<result name="ValueStack">/ognl/ValueStackAction.jsp</result>
<result name="ognl">/ognl/OgnlAction.jsp?msgxx=${msg}</result>
</action>
</package>
</struts>
struts.xml文件中引入自定义配置文件struts_ognl.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.custom.i18n.resources" value="cn.itcast.converter.converter"></constant>
<!--
配置struts2框架的模式
* 默认值是false,是生产模式
* true是开发模式,需要更多的调试信息
### includes:
### - struts.i18n.reload = true
### - struts.configuration.xml.reload = true
-->
<constant name="struts.devMode" value="true"></constant>
<!--
配置所有资源文件,省略后缀名,如果配置多个资源文件时,用","隔开。不仅是国际化资源文件
* 类型转换器的错误提示资源文件
* 国际化资源文件
* 上传文件的错误提示信息资源文件
-->
<constant name="struts.custom.i18n.resources"
value="cn.itcast.converter.converter,
cn.itcast.i18n.resources,
cn.itcast.upload.fileuploadmessage,
cn.itcast.ognl.resources">
</constant>
<!-- 配置文件上传的总大小 -->
<constant name="struts.multipart.maxSize" value="2097152000"></constant>
<!-- 引入自定义配置文件 -->
<include file="cn/itcast/primer/struts_primer.xml"></include>
<include file="cn/itcast/resulttype/struts_resulttype.xml"></include>
<include file="cn/itcast/pattern/struts_pattern.xml"></include>
<include file="cn/itcast/converter/struts_converter.xml"></include>
<include file="cn/itcast/context/struts_context.xml"></include>
<include file="cn/itcast/i18n/struts_i18n.xml"></include>
<include file="cn/itcast/upload/struts_upload.xml"></include>
<include file="cn/itcast/aop/struts_aop.xml"></include>
<include file="cn/itcast/validate/struts_validate.xml"></include>
<include file="cn/itcast/ognl/struts_ognl.xml"></include>
</struts>
编写Action文件
package cn.itcast.ognl;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
@SuppressWarnings("serial")
public class OgnlAction extends ActionSupport {
private String name = "oname";
private String sex = "male";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String test(){
System.out.println("OgnlAction ************ test()");
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("username", "username_request");
request.setAttribute("password", "password_request");
Map sessionMap = ServletActionContext.getContext().getSession();
sessionMap.put("username", "username_session");
sessionMap.put("password", "password_session");
ServletContext sc = ServletActionContext.getServletContext();
sc.setAttribute("username", "username_application");
sc.setAttribute("password", "password_application");
//方法一:获取ValueStack对象
ValueStack valueStack1 = (ValueStack)request.getAttribute("struts.valueStack");
System.out.println("valueStack = "+valueStack1);
/*
* 插入的内容,实际上是先放置在一个map集合中,又把这个map集合放置在对象栈里
*/
valueStack1.set("error", "error_valueStack");
valueStack1.set("msg", "msg_valueStack");
// valueStack1.getRoot().add(0, new Person());
valueStack1.getRoot().add(0,new Employee());
//方法二:获取ValueStack对象
/*ValueStack valueStack2 = ServletActionContext.getContext().getValueStack();
System.out.println("valueStack = "+valueStack2);*/
return "ognl";
}
}
person .java
package cn.itcast.ognl;
public class Person {
private Integer id;
private String name;
private Integer age;
public Person(){
}
public Person(Integer id,String name,Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
Employee .java
package cn.itcast.ognl;
public class Employee {
private String name = "ename";
private Double salary = 3000.0;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
1.如果访问其他Context中的对象,由于他们不是根对象,所以在访问时,需要添加#前缀。
使用ognl表达式取值
2.OGNL会设定一个根对象(root对象),在Struts2中根对象就是ValueStack(值栈)。如果要访问根对象(即ValueStack)中对象的属性,则可以省略#命名对象,直接访问该对象的属性即可。
深入理解值栈中的 ObjectStack
用法2:集合的投影(过滤)
1、集合的投影(只输出部分属性) collectionName.{ expression }
2、集合的过滤
1) 集合的过滤有以下三种方式:
a.“?#”:过滤所有符合条件的集合,如:users.{?#this.age > 19};
b.“^#”:过滤第一个符合条件的元素,如:users.{^#this.age > 19};
c.“$#”:过滤最后一个符合条件的元素,如:users.{$#this.age > 19} 。
2) this 表示集合中的元素;
3、集合的投影和过滤
投影(过滤)操作返回的是一个集合,可以使用索引取得集合中指定的
元素,如:`users.{?#this.age > 19}[0]`
用法3:构造Map,如#{‘foo1’:‘bar1’, ‘foo2’:‘bar2’}。
这种方式常用在给radio或select、checkbox等标签赋值上
构造Map
OGNL表达式语言(%用法)
“%”符号的用途是在标签的属性值被理解为字符串类型时,告诉执行环境%{}里的是OGNL表达式
OGNL表达式语言($用法)
“$”有两个主要的用途
* 用于在国际化资源文件中,引用OGNL表达式
* 在Struts 2配置文件中,引用OGNL表达式
Ognl表达式取值方式
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="description" content="This is my page">
</head>
<body>
<:ognl.jsp<br>
使用EL表达式取值-----------------------------------------<br>
${requestScope.username}<br>
${sessionScope.username}<br>
${applicationScope.username}<br><br><br><br>
使用Ognl表达式取值-----------------------------------------<br>
1 # 如果访问其他Context中的对象,由于他们不是根对象,所以在访问时,需要添加#前缀。<br>
<s:property value="#request.username"/><br>
<s:property value="#session.username"/><br>
<s:property value="#application.username"/><br><br>
<s:property value="#request['username']"/><br>
<s:property value="#parameters.cid[0]"/><br>
<s:property value="#attr.username"/><br><br>
2 # 如果要访问根对象(即ValueStack)中对象的属性,则可以省略#命名对象,直接访问该对象的属性即可。<br>
<s:property value="msg"/><br><br>
深入理解值栈中的 ObjectStack<br>
<s:property value="name"/><br>
<s:property value="sex"/><br>
<s:property value="age"/><br>
<s:property value="salary"/><br><br>
用法3:构造Map<br>
<s:radio list="#{'01':'男','02':'女'}"></s:radio><br><br><br><br>
%的用法:“%”符号的用途是在标签的属性值被理解为字符串类型时,告诉执行环境%{}里的是OGNL表达式。 <br>
<s:property value="#request.username"/><br>
<s:property value="%{#request.username}"/>%{}是万能用法,无论里面的表达式是不是ognl表达式,都会强制理解为ognl表达式<br><br>
“$”有两个主要的用途<br>
* 用于在国际化资源文件中,引用OGNL表达式<br>
<s:text name="ognl" /><br><br>
* 在Struts 2配置文件中,引用OGNL表达式<br>
<s:property value="#parameters.msgxx[0]"/><br><br>
<s:debug></s:debug>
</body>
</html>
结果: