1. 創建一個dynamic web project(創建時讓它自動生成web.xml文件)
2.引入相關jar包
3.在web.xml中進行配置
(啟動tomcat服務器之後第一個加載的文件就是web.xml)
在配置中添加過濾器:
<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>
4.創建struts的核心文件(struts.xml),將其創建在Java Resources-src文件目錄下,內容為:
<?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>
</struts>
5.同樣在Java Resources-src文件目錄下創建一個Action類,繼承自ActionSupport,並覆蓋父類中的execute方法:
public class HelloWorldAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("執行Action");
return SUCCESS;
}
}
6.在struts.xml中的<struts>標簽中添加:
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="helloworld" class="default package.HelloWorldAction">
<result>/result.jsp</result>
</action>
</package>
</struts>
7.創建視圖(在WebRoot目錄下創建result.jsp):
<body>
This is result.jsp!
</body>
8.調試運行