回憶
Struts2的第一個Action
<?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>
第一步:創建一個Java類
package action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
第二步:在struts.xml配置Action中配置
<?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="testAction" class="action.TestAction" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
說明:
- package:相當於Java項目中的包配置,這是必須的。
- name:必選。爲包去一個名字,可以自定義。
- namespace:可選。默認是/。
- extends:必選。而且現階段必須寫成struts-default。
- action
- name:必選。自定義值,值會和namespace的值構成action的訪問路徑。
- class:必選。Action的全路徑。
- method:可選。默認值是execute,它是Action中藥訪問的方法名。
- result:標籤內容是跳轉路徑/success.jsp
- name:必選。這是action中方法的返回值。
第三步:在web.xml中配置Struts2的過濾器
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SSH_Struts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Struts2過濾器 -->
<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>*.action</url-pattern>
</filter-mapping>
</web-app>