环境:idea2020 struts2 2.5
创建web
file-new moudle创建一个web项目
添加maven 选中项目-右键-add Frameworks Support
添加依赖
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.22</version>
</dependency>
</dependencies>
项目结构
编写代码
编写action
package com.kj.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* 类:消息控制器
* 编写人:kujin
* 创建时间:2020/6/20
* 修改时间:2020/6/20
*/
public class MessageAction extends ActionSupport {
private String title=null;
private String content=null;
@Override
public String execute() throws Exception {
return SUCCESS;
}
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;
}
}
编写拦截器
package com.kj.filter;
import com.kj.action.MessageAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* 类:拦截器:文字过滤
* 编写人:kujin
* 创建时间:2020/6/20
* 修改时间:2020/6/20
*/
public class MyInterceptor extends AbstractInterceptor {
/**
* 执行action的前后操作,动态增强Action的功能
* @param actionInvocation
* @return
* @throws Exception
*/
public String intercept(ActionInvocation actionInvocation) throws Exception {
Object action = actionInvocation.getAction();//获取Action实例
if (action!=null){
//如果action 是messageAction
if (action instanceof MessageAction){
//类型转换
MessageAction messageAction= (MessageAction) action;
//获取内容
String content=messageAction.getContent();
//替换所有不合法的内容
if (content.contains("傻逼")){
content=content.replaceAll("傻逼","**和谐有爱**");
}
if(content.contains("操")){
content=content.replaceAll("操","**草是一种植物**");
}
//返回编辑后的内容
messageAction.setContent(content);
return actionInvocation.invoke();
}else {
return Action.LOGIN;
}
}else {
return Action.LOGIN;
}
}
}
编写struts.xml
创建struts.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="User" extends="struts-default">
<!-- 定义文字过滤-->
<interceptors>
<interceptor name="hexie" class="com.kj.filter.MyInterceptor"></interceptor>
</interceptors>
<action name="public" method="execute" class="com.kj.action.MessageAction">
<result name="success">/success.jsp</result>
<!-- 先进行文字过滤,在跳转页面-->
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="hexie"></interceptor-ref>
</action>
</package>
</struts>
利用堆栈管理多个(升级版)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="User" extends="struts-default">
<interceptors>
<interceptor name="hexie" class="com.kj.filter.MyInterceptor"></interceptor>
<interceptor-stack name="my_Stack">
<!-- struts2 提供的拦截器栈,包含了struts2的很多核心拦截器,必须添加,不然获取不到参数 -->
<interceptor-ref name="defaultStack" />
<!-- 定义文字过滤-->
<interceptor-ref name="hexie"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="public" method="execute" class="com.kj.action.MessageAction">
<result name="success">/success.jsp</result>
<!-- 先进行文字过滤,在跳转页面-->
<interceptor-ref name="my_Stack"></interceptor-ref>
</action>
</package>
</struts>
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 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>
</web-app>
编写界面
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/6/20
Time: 12:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<s:form action="public" method="POST">
<s:textfield name="title" label="标题"/>
<s:textarea rows="5" cols="18" label="内容" name="content"/>
<s:submit value="发表"/>
</s:form>
</body>
</html>
success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/6/20
Time: 13:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功界面</title>
</head>
<body>
标题:<s:property value="title"/>
内容:<s:property value="content"/>
</body>
</html>
运行界面