struts2 拦截器的学习

struts2 拦截器

Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的 拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器。

在这里插入图片描述

拦截器的工作原理如上图,每一个Action请求都包装在一系列的拦截器的内部。拦截器可以在Action执行之前做相似的操作也可以在action执行之后做回收操作。

拦截器API

在Struts2的API中有一个com.opensymphony.xwork2.interceptor包,其中有一些Struts2的内置拦截器对象,他们有不同的功能。所有拦截器都直接或简介地实现Interceptor接口。

Interceptor接口声明了三个方法:

public interface Interceptor extends Serializable {
 
    void destroy();
 
    void init();
 
    String intercept(ActionInvocation invocation) throws Exception;
}

Init()方法拦截器类做必要的初始化操作。 Destroy()方法在拦截器被垃圾回收之前调用,用来回收init方法初始化的资源。 Intercept()是拦截器的主要拦截方法。

自定义拦截器

自定义一个拦截器需要三步:

  • 自定义一个实现Interceptor接口(或者继承自AbstractInterceptor)的类。
  • 在strutx.xml中注册上一步中定义的拦截器。
  • 在需要使用的Action中引用上述定义的拦截器,为了方便也可将拦截器定义为默认的拦截器,这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截。

实例:使用拦截器过滤文字

(1)新建一个项目,将Struts2的环境搭建好,导入依赖

<dependencies>
	<dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.18</version>
    </dependency>
  </dependencies>

(2)在web.xml中配置Struts2的核心控制器,主要代码:

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

(3)新建com.zzuli.action.PublicAction类,并继承ActionSupport类实现Action。主要代码:

public class PublicAction extends ActionSupport {
    private String title;
    private String content;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
    @Override
    public String execute()
    {
        return SUCCESS;
    }
}

(4)新建com.zzuli.interceptor.MyInterceptor类,并继承AbstractInterceptor类,用于实现拦截器功能。主要代码:

public class MyInterceptor extends AbstractInterceptor {


    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        Object action = actionInvocation.getAction();
        if (action != null){
            if (action instanceof PublicAction){

                PublicAction publicAction = (PublicAction) action;
                String content = publicAction.getContent();
                if (content.contains("讨厌")){

                    content = content.replaceAll("讨厌", "喜欢");
                    publicAction.setContent(content);

                }
                return actionInvocation.invoke();

            }else {
                return Action.LOGIN;
            }
        }
        return Action.LOGIN;
    }
}

(5)在struts.xml中配置Action和拦截器。主要代码

<struts>
    <package name="default" extends="struts-default">
       <interceptors>
        <interceptor name="replace" class="com.zzuli.interceptor.MyInterceptor" />
       </interceptors>
        <action name="public" class="com.zzuli.action.PublicAction">
            <result name="success">success.jsp</result>
            <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="replace"/>
        </action>

    </package>
</struts>

(6)新建news.jsp页面,在其中使用Struts2标签,用于提交内容。主要代码:

<s:form action="public" method="post">
    <s:textfield name="title" label="评论标题" />
    <s:textarea name="content" cols="36" rows="6" label="评论内容"/>
    <s:submit value="提交"/>
</s:form>

(7)新建success.jsp页面,在其中使用Struts2标签,用于显示提交后的内容。主要代码:

<body>
    评论如下:
    <hr>
    评论标题:<s:property value="title"/>
    <br>
    评论内容:<s:property value="content"/>
</body>

项目目录结构

在这里插入图片描述

运行结果如下

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值