Struts2<4>值栈与 OGNL
来源:互联网 发布:java正则表达式 . 编辑:程序博客网 时间:2024/05/16 09:51
值栈简介
值栈是对应每个请求对象的一套内存数据的封装,Struts2 会给每个请求创建一个新的值栈。
值栈能够线程安全地为每个请求提供公共的数据存取服务。
OGNL 的引入
OGNL 是对象图导航语言 Object-Graph Navigation Language 的缩写,它是一种功能强大的表达式语言。
OGNL 访问 ValueStack 数据
<s:property value=”account” />
OGNL 访问 ActionContext 数据
访问某个范围下的数据要用#
#parameters 请求参数 request.getParameter(...);
#request 请求作用域中的数据 request.getAttribute(...);
#session 会话作用域中的数据 session.getAttribute(...);
#application 应用程序作用域中的数据 application.getAttribute(...);
#attr 按照 page request session application 顺序查找值
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>HeadFirstStruts2Chap01</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>Struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>Struts2</filter-name> <url-pattern>/*</url-pattern></filter-mapping></web-app>
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.HelloAction"> <result name="success" >success.jsp</result> </action> </package></struts>
HelloAction.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 HelloAction extends ActionSupport {/** * */private static final long serialVersionUID = 1L;@Overridepublic 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", 14);return SUCCESS;}}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><%request.setAttribute("name", "李四(request)");request.setAttribute("age", "12");%></head> <body>获取狭义上的值栈数据:<s:property value="name" /><s:property value="age" /><br /> 请求参数:<s:property value="#parameters.name" /><s:property value="#parameters.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>
0 0
- Struts2<4>值栈与 OGNL
- Struts2-OGNL与值栈
- Struts2使用之值栈与OGNL
- struts2--3.Ognl与值栈
- Struts2使用16 -- 值栈与 OGNL 1 -- 概述
- Struts2使用17 -- 值栈与 OGNL 2 -- 使用
- Struts2使用之值栈与OGNL-使用
- Struts2使用之值栈与OGNL-使用
- Struts2使用之值栈与OGNL-概述
- 关于struts2里的ognl与值栈
- struts2学习笔记(五)值栈与ognl
- struts2 之ognl表达式与值栈(03)
- Struts2-值栈和OGNL
- struts2--值栈以及OGNL
- Struts2的值栈-OGNL
- Struts2 值栈和ognl
- Struts2 值栈和OGNL
- (4)Struts2-OGNL表达式与国际化
- 如何在 Linux 中使用屏幕键盘
- 1003 我要通过!
- HTTP协议
- 1004 成绩排名
- unity学习之GUI基础2
- Struts2<4>值栈与 OGNL
- 敏捷开发之Scrum扫盲篇
- ubuntu 12.04版本交叉编译器安装的相关问题
- hoj 1058 Number Triangles
- uva10913(dp + 回溯)
- 1005 继续(3n+1)猜想
- git发布代码到github过程和常见错误
- Java中使用Jedis操作Redis
- 我的2014,人生的又一个转折