使用拦截器过滤文字

使用拦截器过滤文字

1.we.xml文件的配置

  <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>

2.struts.xml文件的配置

struts>
    <package name="default" namespace="/" extends="struts-default">
        <interceptors>
            <interceptor name="replace" class="com.Interceptors.LetterInterceptor"></interceptor>
        </interceptors>

        <action name="LetterAction" class="com.action.LetterAction">
            <result name="success">/success.jsp</result>
            <result name="login">/index.jsp</result>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="replace"></interceptor-ref>
        </action>
    </package>
</struts>

此处注意一个问题:默认拦截器(defaultStack)必须在自定义拦截器之前,否则获取action对象,对象会没有数据。

3.jsp页面的代码

 <body>
  <form action="LetterAction" namespace="/">
    标题:<input type="text" name="title" ><br>
    内容:<textarea name="content"></textarea><br>
    <input type="submit" name="submit" value="提交">&nbsp;&nbsp;
    <input type="reset" name="reset" value="取消">
  </form>
  </body>

4.action的代码的属性必须与form表单的name属性相同

package com.action;
import com.opensymphony.xwork2.ActionSupport;

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

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
}

5.拦截器里实现文字过滤

public class LetterInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation ai) throws Exception {
        Object object=ai.getAction();
        if (object != null) {
            //instanceof是Java中的二元运算符,左边是对象,右边是类;
            // 当对象是右边类或子类所创建对象时,返回true;否则,返回false。
            if(object instanceof LetterAction){
                LetterAction letterAction=(LetterAction)object;
                //同时在后台输出内容
                String title=letterAction.getTitle();
                System.out.println(title);
                String content=letterAction.getContent();
                System.out.println(content);
                if(content.contains("干")){
                //简单过滤,把“干”改成淦“”
                    content=content.replaceAll("干","淦");
                    letterAction.setContent(content);
                }
                return ai.invoke();
            }else{
                return Action.LOGIN;
            }
        }
        return Action.LOGIN;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值