struts2--实现文字过滤功能

来源:互联网 发布:python用什么编译器 编辑:程序博客网 时间:2024/06/04 00:28

如:



过滤结果:


目的是将内容中出现的”北京“替换成"**"

1、过滤器:

package com.Interceptor;import com.action.FilterAction;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class FilterContext extends AbstractInterceptor{@Overridepublic String intercept(ActionInvocation invocation) throws Exception {        Object action = invocation.getAction();        if(action != null){         if(action instanceof FilterAction){             FilterAction filterAction = (FilterAction)action;             String context = filterAction.getContext();             if(context.contains("北京")){             context = context.replaceAll("北京", "**");             filterAction.setContext(context);             }             return "success";             }             }        return "login";}  }

2、Action:

package com.action;import com.opensymphony.xwork2.ActionSupport;public class FilterAction extends ActionSupport {   private String head;   private String context;      public String  execute() {return SUCCESS;}public String getHead() {return head;}public void setHead(String head) {this.head = head;}public String getContext() {return context;}public void setContext(String context) {this.context = context;}}

3、filter.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix = "s" uri = "/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'filter.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>   <s:form action = "filter" method = "post">     <s:textfield name = "head" label = "标题" ></s:textfield>     <s:textarea name = "context" label = "内容" rows="10" cols="40"></s:textarea>     <s:submit value = "过滤" ></s:submit>   </s:form>  </body></html>

4、filter_ok.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix = "s" uri = "/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'filter_ok.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>   标题:<s:property value = "head"/><br><br>   内容:<s:property value = "context"/>  </body></html>


5、struts.xml配置:

     <interceptors>      <interceptor name="filterInterceptor" class="com.Interceptor.FilterContext"/>    </interceptors>    
<action name="filter" class = "com.action.FilterAction">       <result>/filter_ok.jsp</result>       <result name = "login">/filter.jsp</result>       <interceptor-ref name="defaultStack"/>       <interceptor-ref name="filterInterceptor"/>     </action>



原创粉丝点击