一、创建一个动态Web项目
二、配置Web.xml的过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2</display-name>
<!-- 过滤器的配置 -->
<filter>
<filter-name>struts2</filter-name> <!-- 自定义过滤器名 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 过滤器类 (在导入的包中,下面会提到)-->
</filter>
<filter-mapping>
<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>
三、导入相应的jar包
要导入的包:(共9个)
commons-fileupload(上传下载包)
commons-io(输入输出包)
commons-lang 3-3.2(基础包)
commons-logging(日志包)
freemarker(模板引擎,通过模板生成文本输出的通用工具)
structs2-core(核心包)
xwork-core(一些类基于xwork)
ognl(表达式)
javassist-3.11.0.GA.jar(解析java类文件的一个包)
(1)放入lib下
(2)构建路径(将lib下的9个包导入)
四、在src在创建xml文件(struts)
<?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="default" namespace="/" extends="struts-default">
<action name="helloworld" class="HelloWroldAction"> <!-- class为action类 接下来创建-->
<result>/result.jsp</result>
<!--当result 其他属性没有设置,默认返回的结果为SUCCESS(将会跳转到result.jsp页面)-->
</action>
</package>
</struts>
五、在src下创建action类(继承自ActionSupport(来自xwork-core包))
(1)重写execute方法
import com.opensymphony.xwork2.ActionSupport;
public class HelloWroldAction extends ActionSupport {
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行Action");
return SUCCESS;
}
}
六、创建result.jsp页面
(1)根据(标题四)可知返回的结果SUCCESS将会result.jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This a test about Struts2;
</body>
</html>
七、测试
在地址栏中输入? http://localhost:8080/Struts2/helloworld.action?? 访问struts.xml文件下配置的action
结果:出现 This a test about Struts2;
转载请注明原文地址: https://ju.6miu.com/read-678941.html