1.maven创建web项目Artifact Id 为项目名称
2.pom.xml添加stryts http://mvnrepository.com/artifact/org.apache.struts/struts2-core
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>HelloWorld</display-name>
<!-- <init-param>
<param-name>config</param-name>
<param-value>../../resources/struts.xml</param-value>
</init-param> -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置核心拦截器 -->
<filter>
<!-- Filter的名字 -->
<filter-name>struts2</filter-name>
<!-- Filter的实现类 struts2.5以前dispatcher.filter---dispatcher.ng.filter. -->
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<!-- 拦截所有的url -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4.struts.xml 位置在src.main.resource
<?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="default" namespace="/" extends="struts-default">
<!-- name:action的名字,访问时使用 http://localhost:8080/项目名/helloworld访问,class:实现类 -->
<action name="helloworld" class="com.st.HelloWorldAction">
<!-- 结果集,即action中SUCCESS返回的视图 -->
<result>
/result.jsp
</result>
</action>
</package>
</struts>
5.HelloWorldAction.java src文件夹右键new - source folder 新建src.main.java(有就不用了)
src.main.java下新建package: com.st calss: HelloWorldAction
HelloWorldAction.java内容:
package com.st;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("Action启动");
return SUCCESS;
}
}
6.result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Action Result</title>
</head>
<body>
<h1>struts2 is ok</h1>
</body>
</html>
结构: