流程:
- 导入jar包
- 配置web.xml
- 编写action类
- 配置struts.xml
1.使用eclipse创建项目
File》New》Dynamic web project [记得选择生成web.xml文件,会在WebContent目录下生成]
2.在WebContent》WEB-INF>lib中导入包,并build path
下载地址:https://download.csdn.net/download/douzhenwen/11225684
注:一定要build path,鼠标移到包上右击鼠标会出现build path选项
3.在web.xml文件中配置struts过滤器(jsp页面在webcontent下新建jsp目录存放,故在web.xml中将index.jsp的路径修改)
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>jsp/index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置 Struts2 的 Filter -->
<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>
</web-app>
4.创建action类
package com.adu.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
private String message;
@Override
public String execute() throws Exception {
message = "Hello World,Struts2";
return "success";
}
public String getMessage(){
return message;
}
}
5.配置struts.xml文件(在src目录下新建strut.xml文件,dtd约束不可少,即第二第三行的代码)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="action" namespace="/" extends="struts-default">
<action name="HWAction" class="com.adu.action.HelloWorld">
<result name="success">jsp/first.jsp</result>
</action>
</package>
</struts>
说明:jsp目录下有两个jsp文件,第一个为index.jsp首页,第二个为first.jsp文件,index.jsp跳转到first.jsp
项目demo链接:https://download.csdn.net/download/douzhenwen/11225691