struts2
如何使用struts2,或者自定义。特别注意,在使用的时候,在Action里面必须最后一定要引用struts2自带的缺省堆栈defaultStack,如下(这里我是引用了struts2自带的checkbox):
<interceptor-ref>
<param>0</param>
</interceptor-ref>
<interceptor-ref/>(必须加,否则出错)
也可以改为对全局Action设置自己需要的,如下:
在struts.xml里面定义全局的配置设置
<packageextends="struts-default">
<interceptors>
<interceptor-stackcolor: #ff0000;">myStack">
<interceptor-ref>
<param>0</param>
</interceptor-ref>
<interceptor-ref/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref/>(这句是设置所有Action自动调用的堆栈)
</package>
struts-action.xml里面配置Action如下:
<packageextends="struts-shop">(这里扩展struts.xml里面定义的配置就可以了)
<action>
<result>/jsp/smeishop/admin/index.jsp</result>
<result>/jsp/smeishop/admin/logon.jsp</result>
<result>/jsp/smeishop/admin/logon.jsp</result>
</action>
<action>
<result>/jsp/smeishop/admin/logon.jsp</result>
</action>
</package>
你的可以正常工作了!
这里必须继承自struts-default,因为struts2中默认的都配置在struts-default.xml文件中
贴出struts-default.xml文件中的一部分,有兴趣的可以参考以下链接(官方讲解的比较全):
?

Struts2 [Interceptor]

的工作原理如上图,每一个Action请求都包装在一系列的的内部。可以在Action执行直线做相似的操作也可以在Action执行直后做回收操作。
每一个Action既可以将操作转交给下面的,Action也可以直接退出操作返回客户既定的画面。
如何自定义一个?
自定义一个需要三步:
1自定义一个实现Interceptor接口(或者继承自AbstractInterceptor)的类。
2在strutx.xml中注册上一步中定义的。
3在需要使用的Action中引用上述定义的,为了方便也可将定义为默认的,这样在不加特殊声明的情况下所有的Action都被这个拦截。
Interceptor接口声明了三个方法:
public intece Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
}
Init方法在类被创建之后,在对Action镜像拦截之前调用,相当于一个post-constructor方法,使用这个方法可以给类做必要的初始话操作。
Destroy方法在被垃圾回收之前调用,用来回收init方法初始化的资源。struts2
Intercept是的主要拦截方法,如果需要调用后续的Action或者,只需要在该方法中调用invocation.invoke()方法即可,在该方法调用的前后可以插入Action调用前后需要做的方法。如果不需要调用后续的方法,则返回一个String类型的对象即可,例如Action.SUCCESS。
另外AbstractInterceptor提供了一个简单的Interceptor的实现,这个实现为:
public abstract class AbstractInterceptor implements Interceptor {
public void init() {
}
public void destroy() {
}
public abstract String intercept(ActionInvocation invocation) throws Exception;
}
在不需要编写init和destroy方法的时候,只需要从AbstractInterceptor继承而来,实现intercept方法即可。
我们尝试编写一个Session过滤用的,该查看用户Session中是否存在特定的属性(LOGIN属性)如果不存在,中止后续操作定位到LOGIN,否则执行原定操作,代码为:
public class CheckLoginInterceptor extends AbstractInterceptor {
public static final String LOGIN_KEY = "LOGIN";
public static final String LOGIN_PAGE = "global.login";
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("begin check login interceptor!");
//对LoginAction不做该项拦截
Object action = actionInvocation.getAction();
if (action instanceof LoginAction) {
System.out.println("exit check login, because this is login action.");
return actionInvocation.invoke();
}
//确认Session中是否存在LOGIN
Map session = actionInvocation.getInvocationContext().getSession();
String login = (String) session.get(LOGIN_KEY);
if (login != null && login.length() > 0) {
//存在的情况下进行后续操作。
System.out.println("already login!");
return actionInvocation.invoke();
} else {
//否则终止后续操作,返回LOGIN
System.out.println("no login, forward login page!");
return LOGIN_PAGE;
}
}
}
注册
<interceptors>
<interceptor
name="login"
class="com.jpleasure.teamware.util.CheckLoginInterceptor"/>
<interceptor-stack>
<interceptor-ref/>
<interceptor-ref/>
</interceptor-stack>
</interceptors>
将上述设定为默认:
<default-interceptor-ref/>
这样在后续同一个package内部的所有Action执行之前都会被login拦截。
Struts2(XWork)提供的的功能说明:
注册并引用Interceptor
<package extends="struts-default">
<interceptors>
<interceptor/>
<interceptor/>
</interceptors>
<action>
<interceptor-ref/>
<interceptor-ref/>
<result>login.jsp</result>
<result
type="redirect-action">/secure/home</result>
</action>
</package>
可以将多个合并在一起作为一个堆栈调用,当一个堆栈被附加到一个Action的时候,要想Action执行,必须执行堆栈中的每一个。
<package extends="struts-default">
<interceptors>
<interceptor/>
<interceptor/>
<interceptor-stack>
<interceptor-ref/>
<interceptor-ref/>
</interceptor-stack>
</interceptors>
<action>
<interceptor-ref/>
<result>login.jsp</result>
<result
type="redirect-action">/secure/home</result>
</action>
</package>
上述说明的在默认的Struts2应用中,根据惯例配置了若干个堆栈,详细情参看struts-default.xml
其中有一个堆栈比较特殊,他会应用在默认的每一个Action上。
<interceptor-stack>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref>
<param>dojo"..*</param>
</interceptor-ref>
<interceptor-ref/>
<interceptor-ref>
<param>input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref>
<param>input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
每一个都可以配置参数,有两种方式配置参数,一是针对每一个定义参数,二是针对一个堆栈统一定义所有的参数,例如:
<interceptor-ref>
<param>myValidationExcudeMethod</param>
</interceptor-ref>
<interceptor-ref>
<param>myWorkflowExcludeMethod</param>
</interceptor-ref>
或者
<interceptor-ref>
<param>myValidationExcludeMethod</param>
<param>myWorkflowExcludeMethod</param>
</interceptor-ref>
每一个都有两个默认的参数:
excludeMethods -过滤掉不使用的方法和
includeMethods –使用的方法。
需要说明的几点:
1执行的顺序按照定义的顺序执行,例如:
<interceptor-stack>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
</interceptor-stack>
的执行顺序为:
thisWillRunFirstInterceptor
thisWillRunNextInterceptor
followedByThisInterceptor
thisWillRunLastInterceptor
MyAction1
MyAction2 (chain)
MyPreResultListener
MyResult (result)
thisWillRunLastInterceptor
followedByThisInterceptor
thisWillRunNextInterceptor
thisWillRunFirstInterceptor
2使用默认配置每个Action都需要的堆栈,例如:
<actionclass="tutorial.Login">
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
<result>login.jsp</result>
<result type="redirect-action">/secure/home</result>
</action>
可以按照如下的方式定义:
<interceptors>
<interceptor-stack>
<interceptor-ref/>
<interceptor-ref/>
<interceptor-ref/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref/>
<actionclass="tutorial.Login">
<result>login.jsp</result>
<result type="redirect-action">/secure/home</result>
</action>
3如何访问HttpServletRequest,HttpServletResponse或者HttpSession
有两种方法可以达到效果,使用ActionContext:
Map attibutes = ActionContext.getContext().getSession();
或者实现相应的接口:
HttpSessionSessionAware
HttpServletRequestServletRequestAware
HttpServletResponseServletResponseAware
参考文章:【1】
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-49616-1.html
傻大个简直~南海人工岛礁没多久就会部署防御性武器了
公布双降的日期时间点