一、自定义拦截器
1.1 拦截器的创建
1.1.1 实现拦截器接口
package cn.itcast.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyInterceptor implements Interceptor {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
实现之后,我们的自定义拦截器随着项目的创建进行init,随着项目的销毁进行destroy。
1.1.2 继承默认的AbstractInterceptor类
AbstractInterceptor类是struts2内置的默认Inteceptor的实现,帮我们省去了两个方法destory和init
/*
* Copyright 2002-2006,2009 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.opensymphony.xwork2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
/**
* Provides default implementations of optional lifecycle methods
*/
public abstract class AbstractInterceptor implements Interceptor {
/**
* Does nothing
*/
public void init() {
}
/**
* Does nothing
*/
public void destroy() {
}
/**
* Override to handle interception
*/
public abstract String intercept(ActionInvocation invocation) throws Exception;
}
自定义一个类继承它就行了。
package cn.itcast.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyInterceptor2 extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
1.1.3 继承MethodFilterInterceptor
package cn.itcast.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor3 extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
MethodFilterInterceptor 是继承了AbstractInterceptor,提供了一些具体的功能实现,在intercept方法里会调用doIntercept方法,子类可以在这里边写具体的逻辑代码。
二、拦截器的api
2.1 放行api
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
invocation.invoke();
return null;
}
调用invocation.invoke()方法就行会调用下一个拦截器的doIntercept,然后再次调用invoke 知道所有拦截器调用玩,才会真正调用到action,所以这里如果直接返回的话就算是拦截住了,执行不了action了。
2.2 前置处理和后置处理
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("前置处理");
invocation.invoke();
System.out.println("后置处理");
return null;
}
2.3 不放行
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
//System.out.println("前置处理");
//invocation.invoke();
//System.out.println("后置处理");
return "success";
}
这里直接返回,不执行后续的拦截器以及action,直接交给result处理结果,进行页面的跳转。
三、拦截器配置
3.1 注册拦截器
<package name="DemoAction" namespace="/demo" extends="struts-default">
<interceptors>
<!-- 注册自定义的拦截器 -->
<interceptor name="myInter3" class="cn.itcast.interceptor.MyInterceptor3"></interceptor>
<!--注册拦截器栈(一组多个拦截器) -->
<interceptor-stack name="myStack">
<!-- 该拦截器栈包含了我们自定义的烂机器 -->
<interceptor-ref name="myInter3">
<!-- 拦截方法包括add,delete -->
<param name="includeMethods">add,delete</param>
</interceptor-ref>
<!-- 该拦截器栈还引用包括了系统自带的拦截器 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="demoAction_*" class="cn.itcast.action.DemoAction"
method="{1}">
<!-- 引用拦截器 -->
<interceptor-ref name="myStack"></interceptor-ref>
<result >/hello.jsp</result>
</action>
</package>
自定义拦截器
package cn.itcast.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor3 extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("前置处理");
invocation.invoke();
System.out.println("后置处理");
return "success";
}
}
我们的Action
package cn.itcast.action;
import com.opensymphony.xwork2.ActionSupport;
public class DemoAction extends ActionSupport{
public String add(){
System.out.println("DemoAction_add");
return "success";
}
public String delete(){
System.out.println("DemoAction_delete");
return "success";
}
public String update(){
System.out.println("DemoAction_update");
return "success";
}
public String find(){
System.out.println("DemoAction_find");
return "success";
}
}
四、标签体系
4.1 体系介绍
jsp提供了了自定义标签的功能,apache组织就自己自定义了一套标签,没想到很好用,就起了个霸气的名字叫jstl,也就是java标准标签库,之后也被纳入了java的标准库中。struts2开发团队也自己自定义了一套标签,也就是struts2的标签库。
4.2 struts2的标签结构
4.2.1 控制标签
我们先写入数据
public class DemoAction extends ActionSupport{
public String add(){
System.out.println("DemoAction_add");
List<String> list = new ArrayList<String>();
list.add("tom");
list.add("jerry");
list.add("jack");
list.add("rose");
list.add("hqy");
ActionContext.getContext().put("list", list);
return "success";
}
}
然后在jsp页面你上使用标签
<body>
hello,jsp
<br>
<s:iterator value="#list">
<s:property />
</s:iterator>
</body>
输出
4.2.2 数据标签
<body>
hello,jsp
<br>
集合的大小为:
<s:property value="#list.size()"/>
</body>
4.2.3 表单标签
struts2虽然提供了很多表单标签,让我们后端人员更好的使用html,但是现在已经不常用了,就暂时不介绍了。
4.2.4 特殊标签
actionerror标签可以让我们再Action中添加一个错误并显示到页面上
public class DemoAction extends ActionSupport{
public String add(){
this.addActionError("来自手动设置的action错误");
return "success";
}
}
<body>
hello,jsp
错误信息:<s:actionerror/>
</body>
输出
五、总结
今天我们主要学了struts2的拦截器和标签,在开发中有很大的作用。至此,我们四天的struts2的教程也学完了,相信里边也有很多东西没有提及到,这需要我们在开发的时候去自己发现。