`

struts2拦截器小结(1)以及ValueStack和StackContext的一点细节

阅读更多
1.使用自定义拦截器来判断用户提交action时是否登录
public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		//如果是登录或注册请求,跳过拦截器
		String actionName=invocation.getInvocationContext().getName();
		if (actionName.startsWith("login")||actionName.startsWith("regist")) {
            return invocation.invoke();
		} else {
			HttpSession sess = ServletActionContext.getRequest().getSession();
			String username = (String) sess.getAttribute("username");
			if (username == null || username.trim().equals("")) {
                ActionSupport as=(ActionSupport)invocation.getAction();
                as.addActionError(as.getText("error.login.first"));
				return "index";
			}
			return invocation.invoke();
		}
	}


如果验证过程要发送actionerror或actionmessage等消息,如果验证过程要使用国际化资源文件内容等等问题怎么办?
可以通过ActionInvocation的getAction方法来获得一个Action对象,再把它强制转化为struts2为我们提供的ActionSupport对象,这样,就可以在拦截器中实现在Action中一样的操作,调用addActionError等方法。

2.关于ValueStack 和 StackContext

官网说明。
http://www.vaannila.com/struts-2/struts-2-tutorial/struts-2-framework-tutorial-1.html
引用

In Struts 2 the action resides on the ValueStack which is a part of the ActionContext. ActionContext is a global storage area that holds all the data associated with the processing of a request.

When a request comes the params interceptor helps in moving the request data to the ValueStack.

Now the OGNL does the job of converting the string based form data to their corresponding java types. OGNL does this by using the set of available built-in type converters.

Again when the results are generated the OGNL converts the java types of the property on the ValueStack to the string-based HTML output.

ActionContext is thread local which means that the values stored in the ActionContext are unique per thread, this makes the Struts 2 actions thread safe.

这里只大概介绍了下:
ValueStack是存放Action有关的数据,而ActionContext则是存放所有与用户请求相关的数据,当然也就包括ValueStack。
当用户发起一个请求的时候,会被params拦截器取出请求数据放入ValueStack中。
ONNL能提供类型转换的功能,将浏览器的基于文本的数据转化为java数据,反之亦然。
ActionContext使用的是“本地线程”(参考:http://baike.baidu.com/view/4498010.htm),是线程安全的。

struts2中,将ValueStack设置为OGNL的根,所以,可以用OGNL直接访问ValueStack中的对象,如果是存放在非根栈ActionContext中的对象,如session,request等就需要加上“#”号,可以在页面中使用<s:debug/>来查看两个存储区域中的对象数据。
如果不通过OGNL来访问ValueStack或ActionContext中的对象,则可以通过request的getAttribute()方法来访问,我是这样理解的,因为requst包含ActionContext包含ValueStack,不知道对不对。
如果还有其他的一些信息,欢迎补充。

转载请注明出处:http://383984216-qq-com.iteye.com/admin/blogs/1150029
1
3
分享到:
评论
2 楼 liguocai2009 2012-04-12  
很好,对我很有帮助
1 楼 仅此而已 2011-08-16  
谢谢分享!

相关推荐

    Struts2 in action中文版

    第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 为什么使用框架 8 1.3 Struts 2框架...

    Struts2中关于ValueStack的一些操作

    1、 ValueStack其实就是一个放置Java对象的堆栈而已,唯一特别的是可以使用EL来获得值堆栈中对象属性的数据,并可以为值堆栈的对象属性赋值。 2、 EL,全称Express Language,即表达式语言。不要被语言吓倒,它是...

    Struts2入门教程(全新完整版)

    1.概述strust2中的拦截器 28 2.自定义拦截器 28 方式一,实现Interceptor接口。 28 方式二、继承AbstractInterceptor抽象类 29 方式三、继承MethodFilterInteceptor类 30 3.使用来MethodFilterInterceptor灵活拦截 ...

    linjin101#javaStudy#Struts2中的OGNL和值栈ValueStack1

    1、什么是值栈 2、值栈的内部结构 3、ActionContext和ValueStatck的关系 4、如何获取值栈对象 5、向值栈存数据 6、从值栈中获取数据

    Struts中的ognl和valueStack

    深入讲解Struts中的ognl和valueStack

    Struts2 Value Stack Contents 中取值、多个集合数组

    Struts2 Value Stack Contents 中取值、多个集合数组示例

    Struts2练习Demo以及随笔

    Struts的开发步骤、OGNL、ValueStack、Action核心、Result基本原理、Struts2核心标记库、Struts2拦截器、自定义拦截器、UI标记、非UI标记、资源文件国际化等等实例全面使用。

    马士兵Struts2笔记2013

    建立一个Struts2 工程,用Action的属性接收参数,使用Domain Model (实体模型) 接收参数,Struts2_2.1.6版本的中文问题,Struts模块包含,Struts简单数据验证 ,Struts ValueStack(值栈) Debug,Value Stack ...

    Struts用的ognl和valueStack(vs)实例

    Struts用的ognl和valueStack(vs)实例

    Struts2中的参数传递

    我们知道,Struts2完成参数传递处理工作的基础是OGNL和ValueStack。而在这个 过程中,我也把Struts2所要做的工作大致归纳为两个方面: 1. 对OGNL操作进行封装,完成OGNL表达式所表示的值到Java对象的值传递机制 2. ...

    精讲Struts2

    精简全面讲解Struts 2.0Struts2产生背景 工作原理 实现步骤 标签 命名空间 转换器 国际化 输出 令牌机制 拦截器 表单校验(jQuery.validate验证框架) ...OGNL(ActionContext StackContext ValueStack区别)

    Struts2框架基础 二

    OGNL表达式 interceptor拦截器 valuestack的存入 /取出

    struts2 学习例子

    Struts2标签库提供了主题、模板支持,极大地简化了视图页面的编写,而且,struts2的主题、模板都提供了很好的扩展性。实现了更好的代码复用。Struts2允许在页面中使用自定义组件,这完全能满足项目中页面显示复杂,...

    struts2 标签库使用文档

    Struts2标签库的组成 Struts2框架的标签库可以分为以下三类: 用户界面标签(UI标签):主要用来生成HTML元素的标签。 表单标签:主要用于生成HTML页面的FORM元素,以及普通表单元素的标签。 非表单标签:主要用于生成...

    struts.docx

    Struts2是处理访问服务器的请求 学习struts2的(重点) Struts.xml的配置 参数传递 Ognl ValueStack Action原理 Result原理 拦截器 struts将会完全的取代我们的servlet技术

    Struts2源码分析之ParametersInterceptor拦截器

    主要介绍了Struts2源码分析之ParametersInterceptor拦截器,ParametersInterceptor拦截器其主要功能是把ActionContext中的请求参数设置到ValueStack中,,需要的朋友可以参考下

    Struts2基础教程

    讲解Struts2入门基础,包括Action、Result、ValueStack等,Struts2初学者会有帮助

    ValueStack使用参考

    ValueStack的使用方法和一些技能

    在线培训:ValueStack

    培训主要学习以下几个知识点: 1、制作PPT,讲解栈的特点和使用方法 2、编码实现回文对 3、制作PPT,讲解Struts2中的ValueStack 4、编码改变ValueStack中的对象的顺序

Global site tag (gtag.js) - Google Analytics