第一节:值栈简介
值栈是对应每个请求对象的一套内存数据的封装,Struts2 会给每个请求创建一个新的值栈。
值栈能够线程安全地为每个请求提供公共的数据存取服务。
第二节:OGNL 引入
OGNL 是对象图导航语言 Object-GraphNavigationLanguage 的缩写,它是一种功能强大的表达式语言。 OGNL 访问 ValueStack 数据 <s:propertyvalue=”account”/>
OGNL 访问 ActionContext 数据 访问某个范围下的数据要用#
#parameters 请求参数 request.getParameter(...);
#request 请求作用域中的数据 request.getAttribute(...);
#session 会话作用域中的数据 session.getAttribute(...);
#application 应用程序作用域中的数据 application.getAttribute(...);
#attr 按照 page request session application 顺序查找值
HelloWorldAction.java
package com.java1234.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class HelloWorldAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = -3761283067880085163L;
public HelloWorldAction() {
System.out.println(this);
}
@Override
public String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext();
//获取狭义上的值栈
ValueStack valueStack = actionContext.getValueStack();
valueStack.set("name", "张三(valueStack)");
valueStack.set("age", 11);
Map<String, Object> session = actionContext.getSession();
session.put("name", "王五(session)");
session.put("age", 13);
Map<String, Object> application = actionContext.getApplication();
application.put("name", "赵六application");
application.put("age", 16);
return SUCCESS;
}
}
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>
<package name="manage" namespace="/" extends="struts-default">
<action name="hello" class="com.java1234.action.HelloWorldAction">
<result name="success" >success.jsp</result>
</action>
</package>
</struts>
页面:success.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>Insert title here</title>
</head>
<%
request.setAttribute("name", "李四request");
request.setAttribute("age", "12");
%>
<body>
获取侠义上的值栈数据:<s:property value="name"/>
<s:property value="age"/><br>
请求参数<s:property value="#paramters.name"/>
<s:property value="#paramters.age"/><br>
request<s:property value="#request.name"/>
<s:property value="#request.age"/><br>
session<s:property value="#session.name"/>
<s:property value="#session.age"/><br>
application<s:property value="#application.name"/>
<s:property value="#application.age"/><br>
attr<s:property value="#attr.name"/>
<s:property value="#attr.age"/><br>
</body>
</html>
运行结果:
第三节:OGNL 访问复杂对象
1,访问 javaBean 对象
ognl访问javaBean对象:<s:property value="student.name"/>
<s:property value="student.age"/>
2,访问集合对象;
ognl访问List对象
<s:property value="students[0].name"/>
<s:property value="students[0].age"/>
<s:property value="students[1].name"/>
<s:property value="students[1].age"/>
3,访问 Map 对象
ognl访问访问Map
<s:property value="studentMap['goodStudent'].name"/>
<s:property value="studentMap['goodStudent'].age"/>
<s:property value="studentMap['badStudent'].name"/>
<s:property value="studentMap['badStudent'].age"/>
第四节:OGNL 访问静态方法和属性
1,访问静态属性
<s:property value="@com.java1234.common.MyStatic@str"/>
2,访问静态方法
struts.xml配置文件需要加上下列配置项,表示允许访问静态方法
<constantname="struts.ognl.allowStaticMethodAccess"value="true"></constant>
<s:property value="@com.java1234.common.MyStatic@printUrl()"/>//有返回值的数据也能直接写入页面