• Struts2自定义拦截器——完整实例代码


    比如一个网上论坛过滤系统,将网友发表的不文明、不和谐的语言,通过拦截器对这些文字进行自动替代。

    该项目包含:

    1、自定义拦截器(MyInterceptor.java)

    2、发表评论的页面(news.jsp)

    3、对应的业务控制器(PublicAction.java)

    4、业务控制器控制其跳转到success.jsp

    代码如下:

    1、自定义拦截器(MyInterceptor.java)

    package interceptor;
    
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    
    
    public class MyInterceptor extends AbstractInterceptor{
    
        public String intercept(ActionInvocation ai) throws Exception {
            //获取Action的实例
            Object object = ai.getAction();
            if(object!=null){
                if(object instanceof PublicAction){
                    PublicAction ac=(PublicAction)object;
                    
                    //获取用户提交的评论内容
                    String content=ac.getContent();
                    //判断用户提交的评论内容是否有要过滤的内容
                    if(content.contains("讨厌")){
                        //以“喜欢”代替要过滤的“讨厌”
                        content = content.replaceAll("讨厌","喜欢");
                        //把替代后的评论内容设置为Action的评论内容
                        ac.setContent(content);
                    }
                    //对象不空,继续执行
                    return ai.invoke();
                    
                }else{
                    //返回Action中的LOGIN逻辑视图字符串
                    return Action.LOGIN;
                }
            }
            
            return Action.LOGIN;
        }
    
    }

    2、发表评论的页面(news.jsp)

    <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        请发表你的评论:
        <hr>
        <s:form action="public" method="post">
            <s:textfield name="title" label="评论标题" maxLength="36" />
            <s:textarea name="content" rows="6" cols="36" label="评论内容" />
            <s:submit value="提交" />
        </s:form>
        
        
    </body>
    </html>

    3、对应的业务控制器(PublicAction.java)

    package interceptor;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    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;
        }
        
        public String execute() throws Exception{
            return "success";
        }
        
        
    }

    4、业务控制器控制其跳转到success.jsp

    <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        评论如下:
        <hr>
        评论标题:<s:property value="title" />
        <br>
        评论内容:<s:property value="content" />
        
        
    </body>
    </html>

    5、struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC 
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        
        
            
        
        <package name="intercept" extends="struts-default">
            <interceptors>
                <interceptor name="replace" class="interceptor.MyInterceptor" ></interceptor>
            </interceptors>
            
            
            <action name="public" class="interceptor.PublicAction">
                <result name="success">/success.jsp</result>
                <result name="login">/success.jsp</result>
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <interceptor-ref name="replace"></interceptor-ref>
            </action>
            
            
            
        </package>
        
    </struts>

    6、web.xml

    <?xml version="1.0" encoding="UTF-8"?>  
    <web-app  version="2.4" 
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
    
      
        <filter>  
            <!-- 配置Struts2核心Filter的名字 -->  
            <filter-name>struts2</filter-name>  
            <!-- 配置Struts2核心Filter的实现类 -->  
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
        </filter>  
        <!-- 配置Filter拦截的URL -->  
        <filter-mapping>  
            <!-- 配置Struts2的核心FilterDispatcher拦截所有用户请求 -->  
            <filter-name>struts2</filter-name>  
            <url-pattern>/*</url-pattern>  
        </filter-mapping>  
      
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list> 
    </web-app>  
  • 相关阅读:
    nginx相关
    facebook开源项目集合
    鸡汤有毒--大家多读
    曹政--互联网搜索老师傅
    将jar文件加到Maven的local repository中
    java web classpath
    java 读取excel内容转为JSONArray
    (.DS_Store)避免多人提交代码到GitHub上起冲突
    mvn dependency:tree
    Java Web乱码分析及解决方案
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/5673541.html
Copyright © 2020-2023  润新知