随笔-34  评论-1965  文章-0  trackbacks-0

首先,要跟大家道个歉,前一阵子为给客户个一个DEMO,忙得不可开交,所以很久没有更新Blog。提到这个DEMO我想顺便跟大家分享一下心得——如果大家希望快速开发,一个类似Struts 2这样的简单方便的WEB框架必不可少。我们在开发DEMO使用的还是Struts 1.2.8,而且没有不使用任何EL(表达式语言),导致页面出现无数类似“<%= ((Integer) request.getAttribute("xx")).intValue()%6 %>”的代码。Struts 1.x的Form Bean的麻烦使得有部分同事直接使用request.getParameter(String arg),继而引入另一种麻烦。诸如此类的问题,在DEMO这样时间紧迫的项目凸显了Struts 1.x对快速开发的无能为力。不过没办法,由于我们项目中的几个资深员工除了Struts 1.x外,对其它的WEB框架似乎不大感兴趣。

言归正传,Interceptor(以下译为拦截器)是Struts 2的一个强有力的工具,有许多功能(feature)都是构建于它之上,如国际化转换器校验等。

什么是拦截器

拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。

在Webwork的中文文档的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。

谈到拦截器,还有一个词大家应该知道——拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。

实现原理

Struts 2的拦截器实现相对简单。当请求到达Struts 2的ServletDispatcher时,Struts 2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表(list),最后一个一个地调用列表中的拦截器,如图1所示。

图1 拦截器调用序列图
图1 拦截器调用序列图

已有的拦截器

Struts 2已经为您提供丰富多样的,功能齐全的拦截器实现。大家可以到struts2-all-2.0.1.jar或struts2-core-2.0.1.jar包的struts-default.xml查看关于默认的拦截器与拦截器链的配置。

在本文使用是Struts 2的最新发布版本2.0.1。需要下载的朋友请点击以下链接:
http://apache.justdn.org/struts/binaries/struts-2.0.1-all.zip

以下部分就是从struts-default.xml文件摘取的内容:

< interceptor name ="alias" class ="com.opensymphony.xwork2.interceptor.AliasInterceptor" />
< interceptor name ="autowiring" class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" />
< interceptor name ="chain" class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" />
< interceptor name ="conversionError" class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" />
< interceptor name ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" />
< interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
< interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" />
< interceptor name ="execAndWait" class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" />
< interceptor name ="exception" class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" />
< interceptor name ="fileUpload" class ="org.apache.struts2.interceptor.FileUploadInterceptor" />
< interceptor name ="i18n" class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" />
< interceptor name ="logger" class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" />
< interceptor name ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />
< interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" />
< interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" />
< interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" />
< interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" />
< interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" />
< interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" />
< interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" />
< interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" />
< interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" />
< interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
< interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" />
< interceptor name ="workflow" class ="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" />
< interceptor name ="store" class ="org.apache.struts2.interceptor.MessageStoreInterceptor" />
< interceptor name ="checkbox" class ="org.apache.struts2.interceptor.CheckboxInterceptor" />
< interceptor name ="profiling" class ="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />

配置和使用拦截器

在struts-default.xml中已经配置了以上的拦截器。如果您想要使用上述拦截器,只需要在应用程序struts.xml文件中通过“<include file="struts-default.xml" />”将struts-default.xml文件包含进来,并继承其中的struts-default包(package),最后在定义Action时,使用“<interceptor-ref name="xx" />”引用拦截器或拦截器栈(interceptor stack)。一旦您继承了struts-default包(package),所有Action都会调用拦截器栈 ——defaultStack。当然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆盖defaultStack。

下面是关于拦截器timer使用的例子。首先,新建Action类tuotrial/TimerInterceptorAction.java,内容如下:

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class TimerInterceptorAction extends ActionSupport {
   @Override
   
public String execute() {
       
try {
           
// 模拟耗时的操作
           Thread.sleep( 500 );
       }
catch (Exception e) {
           e.printStackTrace();
       }

       
return SUCCESS;
   }

}

配置Action,名为Timer,配置文件如下:

<! DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
   
< include file ="struts-default.xml" />    
   
< package name ="InterceptorDemo" extends ="struts-default" >
       
< action name ="Timer" class ="tutorial.TimerInterceptorAction" >
           
< interceptor-ref name ="timer" />
           
< result > /Timer.jsp </ result >
       
</ action >
   
</ package >
</ struts >

至于Timer.jsp可以随意写些什么到里面。发布运行应用程序,在浏览器的地址栏键入http://localhost:8080/Struts2_Interceptor/Timer.action,在出现Timer.jsp页面后,查看服务器的后台输出。

2006 - 12 - 6 14 : 27 : 32 com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
信息: Executed action
[ //Timer!execute ] took 2859 ms.

在您的环境中执行Timer!execute的耗时,可能上述的时间有些不同,这取决于您PC的性能。但是无论如何,2859 ms与500 ms还是相差太远了。这是什么原因呢?其实原因是第一次加载Timer时,需要进行一定的初始工作。当你重新请求Timer.action时,以上输出会变为:

2006 - 12 - 6 14 : 29 : 18 com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
信息: Executed action
[ //Timer!execute ] took 500 ms.

OK,这正是我们期待的结果。上述例子演示了拦截器timer的用途——用于显示执行某个action方法的耗时,在我们做一个粗略的性能调试时,这相当有用。

自定义拦截器

作为“框架(framework)”,可扩展性是不可或缺的,因为世上没有放之四海而皆准的东西。虽然,Struts 2为我们提供如此丰富的拦截器实现,但是这并不意味我们失去创建自定义拦截器的能力,恰恰相反,在Struts 2自定义拦截器是相当容易的一件事。

 

大家在开始着手创建自定义拦截器前,切记以下原则:
拦截器必须是无状态的,不要使用在API提供的ActionInvocation之外的任何东西。

要求拦截器是无状态的原因是Struts 2不能保证为每一个请求或者action创建一个实例,所以如果拦截器带有状态,会引发并发问题。

所有的Struts 2的拦截器都直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。除此之外,大家可能更喜欢继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor。

以下例子演示通过继承AbstractInterceptor,实现授权拦截器。

首先,创建授权拦截器类tutorial.AuthorizationInterceptor,代码如下:

package tutorial;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthorizationInterceptor extends AbstractInterceptor {

   @Override
   
public String intercept(ActionInvocation ai) throws Exception {
       Map session
= ai.getInvocationContext().getSession();
       String role
= (String) session.get( " ROLE " );
       
if ( null != role) {
           Object o
= ai.getAction();
           
if (o instanceof RoleAware) {
               RoleAware action
= (RoleAware) o;
               action.setRole(role);
           }

           
return ai.invoke();
       }
else {
           
return Action.LOGIN;
       }
       
   }


}

以上代码相当简单,我们通过检查session是否存在键为“ROLE”的字符串,判断用户是否登陆。如果用户已经登陆,将角色放到Action中,调用Action;否则,拦截直接返回Action.LOGIN字段。为了方便将角色放入Action,我定义了接口tutorial.RoleAware,代码如下:

package tutorial;

public interface RoleAware {
   
void setRole(String role);
}

接着,创建Action类tutorial.AuthorizatedAccess模拟访问受限资源,它作用就是通过实现RoleAware获取角色,并将其显示到ShowUser.jsp中,代码如下:

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class AuthorizatedAccess extends ActionSupport implements RoleAware {
   
private String role;
   
   
public void setRole(String role) {
       
this .role = role;
   }

   
   
public String getRole() {
       
return role;
   }


   @Override
   
public String execute() {
       
return SUCCESS;
   }

}

以下是ShowUser.jsp的代码:

<% @ page  contentType = " text/html; charset=UTF-8 " %>
<% @taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
   
< title > Authorizated User </ title >
</ head >
< body >
   
< h1 > Your role is: < s:property value ="role" /></ h1 >
</ body >
</ html >

然后,创建tutorial.Roles初始化角色列表,代码如下:

package tutorial;

import java.util.Hashtable;
import java.util.Map;


public class Roles {
   
public Map < String, String > getRoles() {
       Map
< String, String > roles = new Hashtable < String, String > ( 2 );
       roles.put(
" EMPLOYEE " , " Employee " );
       roles.put(
" MANAGER " , " Manager " );
       
return roles;
   }

}

接下来,新建Login.jsp实例化tutorial.Roles,并将其roles属性赋予<s:radio>标志,代码如下:

<% @ page  contentType = " text/html; charset=UTF-8 " %>
<% @taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
   
< title > Login </ title >
</ head >
< body >
   
< h1 > Login </ h1 >
    Please select a role below:
   
< s:bean id ="roles" name ="tutorial.Roles" />
   
< s:form action ="Login" >
       
< s:radio list ="#roles.roles" value ="'EMPLOYEE'" name ="role" label ="Role" />
       
< s:submit />
   
</ s:form >
</ body >
</ html >

创建Action类tutorial.Login将role放到session中,并转到Action类tutorial.AuthorizatedAccess,代码如下:

package tutorial;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport implements SessionAware {
   
private String role;    
   
private Map session;

   
public String getRole() {
       
return role;
   }


   
public void setRole(String role) {
       
this .role = role;
   }

   
   
public void setSession(Map session) {
       
this .session = session;
   }


   @Override
   
public String execute() {
       session.put(
" ROLE " , role);
       
return SUCCESS;
   }
   
}

最后,配置struts.xml文件,内容如下:

<! DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
   
< include file ="struts-default.xml" />    
   
< package name ="InterceptorDemo" extends ="struts-default" >
       
< interceptors >
           
< interceptor name ="auth" class ="tutorial.AuthorizationInterceptor" />
       
</ interceptors >
       
< action name ="Timer" class ="tutorial.TimerInterceptorAction" >
           
< interceptor-ref name ="timer" />
           
< result > /Timer.jsp </ result >
       
</ action >
       
< action name ="Login" class ="tutorial.Login" >
           
< result type ="chain" > AuthorizatedAccess </ result >
       
</ action >
       
< action name ="AuthorizatedAccess" class ="tutorial.AuthorizatedAccess" >
           
< interceptor-ref name ="auth" />
           
< result name ="login" > /Login.jsp </ result >
           
< result name ="success" > /ShowRole.jsp </ result >
       
</ action >
   
</ package >
</ struts >

发布运行应用程序,在浏览器地址栏中输入:http://localhost:8080/Struts2_Interceptor/AuthorizatedAccess.action。由于此时,session还没有键为“ROLE”的值,所以返回Login.jsp页面,如图2所示:

图2 Login.jsp
图2 Login.jsp

选中Employee,点击Submit,出现图3所示页面:

图3 ShowRole.jsp
图3 ShowRole.jsp

总结

拦截器是Struts 2比较重要的一个功能。通过正确地使用拦截器,我们可以编写高可复用的代码。

posted on 2006-12-06 20:10 Max 阅读(111473) 评论(72)  编辑  收藏 所属分类: Struts 2.0系列

评论:
# re: Struts 2的基石——拦截器(Interceptor) 2006-12-06 20:43 | king[匿名]
一直 在关注你struts2的文章,写的很不错。谢谢!!  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2006-12-06 22:50 | chwen[匿名]
写的不错,通俗易懂!  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2006-12-07 09:31 | Tendy
to Max:

-------
由于我们项目中的几个资深员工除了Struts 1.x外,对其它的WEB框架似乎不大感兴趣。
-------

商业软件,
选择什么框架,
其实他们未必能做主......
  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2006-12-07 11:01 | Max
@Tendy
这个项目我们是可以自由选取框架。因为只是DEMO,不管我们用什么框架(或不用框架),实现所有功能需求就可以了。
小发一下牢骚:)  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2006-12-07 15:58 | lix
有时间大家去读读Ted他们写的struts2的文档和example,会有更多认识,文档写得蛮清楚的  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2006-12-08 09:46 | 张先生
2.0.1还是beta版,究竟啥时发布啊??等....  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-03-23 11:42 | yangdamao
謝謝,正在學習中,感覺不錯!  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-04-16 17:43 | xianglg
配置文件中的
< result name ="success" > /ShowRole.jsp </ result >
应该改为ShowUser.jsp  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-04-19 18:04 | xulao
楼主,你觉得要快速开发一个WEB原型系统,搭什么样的开发环境比较理想  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-04-19 22:47 | Max
@xulao
我个人认为MyEclipse 5.1或NetBean 5.5都不错的。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-04-25 09:17 | ddd
偶用eclipse, MyEclipse不是需要注册的嘛!

大家都是买版权的吗?

关于这个权限拦截的例子, 是否应该在用户键入任何页面时,都调用,

这样的话,要如何实现呢?  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-04-25 14:02 | ddd
LS的问题, 知道了,

是否在每个action定义块里面, 加上< interceptor-ref name ="auth" />
就可以了呢?

如果用户直接指定的url不是action, 也是jsp的话,
该如果调用拦截器呢?  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-05-21 22:30 | yesw
<s:radio list ="#roles.roles" value ="EMPLOYEE" name ="role" label ="Role" /> 是不是设置单选按钮的默认值是"EMPLOYEE"啊,为什么页面上的Employee单选按钮没有被选中???  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-05-22 09:21 | Max
@yesw
用value ="'EMPLOYEE'",就可以了。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-07-31 09:31 | wsc
It good article! Thanks to Max  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor)[未登录] 2007-08-11 15:56 | Steve
HI!Max:
你能不能写一个关于ActionInvocation中addPreResultListener方法的例子(运用在拦截器中的)
最好详细点,我看文档看的晕晕的!!
先谢了啊  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-08-12 15:13 | louleigh
MAX老大。
这个程序我调不通啊

首先是有一个duplicate s的错误。我估计是不是两个jsp都用了
prefix="s"的问题。.后来我把另外一个改成z就好了。
这个问题我已经成功解决.

另外有一个问题就是login.jsp中的
<s:bean id="roles" name="tutorial.Roles">他报错。他说attrbuite no alue.
请问该怎么解决~  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-08-24 17:07 | jun jun
Struts Problem Report

Struts has detected an unhandled exception:
# Messages: No result defined for action login.Login and result input
File: file:/D:/PROJECTS/STRUTS/apache-tomcat-5.5.23/webapps/ROOT/WEB-INF/classes/strutsLogin.xml
Line number: 72
Column number: 44

</action>



<action name="Login" class="login.Login">

<result name="SUCCESS" type="chain">AuthorizateAccess</result>

</action>

我的也是这么做的,为什么要报错呢?  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor)[未登录] 2007-08-29 15:40 | sclsch
mark  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-09-26 11:18 | lasa
谢谢楼主写的这么好的文章。

关于拦截器遇到一个问题。
当我是使用struts2和spring2集成的时候,如果在action加入拦截器,
从A页面提交action 然后在转到B页面的数据就没法得到了。
<action name="HelloWorld" class="helloWorld">
<interceptor-ref name="timer" />
<result>HelloWorld.jsp</result>
</action>
不知道有没有兄弟碰到类似的问题。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-09-27 10:19 | torry
谁能告诉我怎样在拦截器得到action的name?谢谢  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-09-27 10:30 | torry
不好意思,我找到那个函数了,是ActionInvocation.getInvocationContext().getName()可以得到action的name  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-10-14 14:26 | hpyzay
ai.getAction()这个是什么意思啊,< s:radio list ="#roles.roles" value ="'EMPLOYEE'" name ="role" label ="Role" />其中list="#roles.roles"的后一个roles是什么啊 请各位指教  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-11-13 09:19 | rocketwang
在interceptor中可以修改action的属性值吗?  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-11-13 16:20 | kinghlc
@lasa
我有碰到这种情况,一直没找到解决的办法,最后不得不放弃使用拦截器进行权限检查  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-11-20 13:42 | 高山流水
@hpyzay---
Object getAction()
Get the Action associated with this ActionInvocation
-------------
list="#roles.roles" == roles.getRoles();  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-12-03 16:26 | 弱弱
弱弱的问下Login那个action里的session是从哪里传进去的?  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-12-07 09:40 | kinghlc
正确使用自定义拦截器的方法:定义好拦截器之后,在定义一个拦截器栈,并继承默认的拦截器栈  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2007-12-21 21:49 | 技术交流
楼主若对j2ee技术感兴趣,我们诚邀您加入我们的技术讨论QQ群!本群加入条件为1年以上java工作经验! 41732384  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-01-11 14:58 | nb
1  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor)[未登录] 2008-01-17 10:56 | 西西
老大,struts.xml里的< result name ="success" > /ShowRole.jsp </ result > 应该是ShowUser.jsp吧;一开始我也是复制的,后来看,这里有点小问题,改过来就好了  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-02-14 17:41 | jammth
请问ai.invoke()执行后返回不到ShowUser.jsp页面是什么原因呢?  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-03-10 14:50 | lastsweetop
加入拦截器 Action中的execute就无法调用  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-03-24 09:35 | oh,no..
不是吧,struts2和struts1.2差别这么大,现在最新的netbeans6.01也只支持struts1.2..  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-04-01 13:46 | guxx
请问如何屏蔽freemarker.template.TemplateException: Error reading included file admin/views/contentAdm.ftl这个异常信息?  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-04-05 17:15 | 开始疯狂
@技术交流
一个qq群搞的给公司招聘一样,还工作经验什么滴。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-04-18 11:47 | ni ba
@wsc
sb  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-04-18 11:48 | ni ba
@lastsweetop
你sb阿  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-04-22 14:59 | billpan100
读完后,想顶一下! 写得很好!   回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor)[未登录] 2008-06-04 15:25 | John
通俗易懂,又有所收获了  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-06-10 11:24 | jiangjkd
< result name ="login" > /Login.jsp </ result >
< result name ="success" > /ShowRole.jsp </ result >

为什么 name 属性值是 login 和 success.这是在哪里规定的呢  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-06-30 10:13 | nuoting
我想问一下:
org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor这个拦截器的源代码在哪里呢?或者它的class文件又在哪里呢?  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor)[未登录] 2008-08-05 15:56 | matrix
挺棒的文章,想学struts2,就来这看看了。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-08-06 14:10 | 代理163
通俗易懂  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-08-09 10:26 | ll
大家都说好。其实也不错。就是少了点。。。
希望多点更底层更本质的struts2的东西、、、谢谢  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-08-11 12:34 | zff
拦截器我是第一次用, 那个roles.java是怎么读到的??  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-08-25 21:46 | jurnzhou
workshop studio...开发struts1.x...不是一般强大...

IDE易用度我觉得足以框架的在维护上的不足...  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-09-25 13:45 | 高举
多谢楼主贡献  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-10-07 21:21 | bb
好文章,我的布署成功了,但是有一个问题:
第一次test:
(1)访问http://127.0.0.1:8081/struts2/Login.action,页面出现正常(供角色选择页),然后选择'Employee',页面显示"Your role is:EMPLOYEE"正常,
(2):不关闭IE把,IE中的Login修改AuthorizatedAccess,我想应该页面应该还是会显示"Your role is:EMPLOYEE",可是出现了角色选择页,与我预见不同。
why?

第二次test:
(1):半闭第一次test ie,输入http://127.0.0.1:8081/struts2/AuthorizatedAccess.action,页面进入角色选择页,这是对的,然后选择'Employee',IE中变成http://127.0.0.1:8081/struts2/Login.action;jsessionid=796BA48C1D997979A39F53C9B665E513,页面出现"Your role is:EMPLOYEE".正常
(2):不关闭IE把,IE中的Login修改AuthorizatedAccess,页面出现"Your role is:EMPLOYEE".正常。
(3):可是只要我把;jsessionid=796BA48C1D997979A39F53C9B665E513去了,就又回到角色选择页了
WHY?
谢谢各位
  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-10-11 00:02 | 毕达哥拉斯
@lasa
你加的拦截器覆盖了缺省的拦截器,所以无法获将提交上来的数据整理到Action中,你可以自己定义拦截器栈,其中包括default的拦截器栈,后面加上你自己的拦截器就ok了。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-10-18 09:39 | bb
谢谢您的回复,我刚学struts2,是个fresh man,您能不能贴上相关的代码配置  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor)[未登录] 2008-11-20 10:33 | 啊啊
<result name ="login" > 和
<action name ="Login" class ="tutorial.Login" >

2各中的name我為什麽不能改,換一個名字就會出錯 為什麽一定是login呢 在哪裏能改呢 誰告訴我下

Login.jsp裏面調action的login也改了
  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor)[未登录] 2008-11-29 10:14 | 初出茅庐
@毕达哥拉斯
你的方法是可行的
可以自己定义自己的拦截器栈 将自己的拦截器和系统的拦截器都放在里面
引用自己的拦截器栈就行了
<interceptors>
<!-- 定义自己的拦截器 -->
<interceptor name="chk" class="com.langwei.roles.common.TestLogin"></interceptor>
<!-- 定义默认的检测器栈 -->
<interceptor-stack name="mydefaultStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="chk"/>
</interceptor-stack>
</interceptors>   回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-12-09 15:54 | icewind
文章很不错,我正在慢慢学习中,只是有几个例子调不通,Max可否吧所有的例子打包发给我,icewind5312@163.com,万分感谢  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2008-12-18 08:12 | 匿名
max说的很好,建议那些不懂的人先看看struts2再说。
  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2009-05-23 15:24 | ly
@kinghlc
对头~~~  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2009-05-23 15:24 | ly
@初出茅庐
关键啊~~~~  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2009-06-19 16:15 | min
看了你写的东西,挺好的,懂老,THANKS。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2009-09-02 01:12 | hu ai
@lasa你是不是用属性传参的。你使用拦截器后,会交黙认的拦截器覆盖。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor)[未登录] 2010-05-14 09:25 | alin
写的真清楚!  回复  更多评论
  
# 非常不错[未登录] 2010-11-14 16:16 |
感谢Max 。时间飞快,我是从chm 中看到的,因为有的图看不到,所以就进到网址来看。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2010-12-08 11:20 | fdxganli
@高山流水
  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2010-12-17 14:36 | ap
@Steve
5293
你可以看看搜索一struts2视频,上面有。youku上找一下。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2010-12-17 14:44 | ap
@啊啊
那个是固定的好像是。result有几个默认值。比如 input success login估计也是,因为你看 那个拦截器那个java文件有一句 return Action.LOGIN; 有不对的地方请指出。我也刚学习不久。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2011-02-09 10:39 |
楼主很专业,小弟 很佩服!  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor)[未登录] 2011-02-15 14:36 | jacky
@毕达哥拉斯
我按照你说的方法试过了,但还是无法获得jsp页面传来的参数  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor)[未登录] 2011-02-15 14:40 | jacky
谢谢各位,终于搞定了,原来在action中要加入<interceptor-ref name="mydefaultStack"/>这句啊……
  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2011-11-13 22:31 | 裂纹
为什么我按照你写的例子去配置拦截器,提示找不到文件呢?我配置了好几个都不行,不晓得为什么求解


type Status report

message /Struts2_Interceptor/

description The requested resource (/Struts2_Interceptor/) is not available.
  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2014-03-01 11:26 | 狄仁杰
写的很不错,看完之后就学会了利用拦截器。  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2014-03-01 11:31 | 狄仁杰
@ddd
可以配置全局拦截器
<default-interceptor-ref name="auth" />
<global-results>
<result name="login">/Login.jsp</result>
</global-results>  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2014-11-14 17:34 | osmond.gao
为什么跳转到点击提交后跳转到Login.action就报500异常了呢  回复  更多评论
  
# re: Struts 2的基石——拦截器(Interceptor) 2014-11-16 17:56 | osmond.gao
为什么我的Login类的两个私有属性一定要初始化才行,不初始化就报500错误
我得写成下面这样才行
public class Login extends ActionSupport implements SessionAware {
private String role = "";//初始化值
private Map<String, String> session = new Hashtable<String, String>();//实例化对象  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: