Struts之Action的訪問方式

NO IMAGE

1.通過method屬性

Action1.java

package zh.action.demo;

import com.opensymphony.xwork2.ActionSupport;

public class Action1 extends ActionSupport{
	
	/**
	 * 返回值為NONE常量,表示沒有返回值,無需配置<result>
	 * @return
	 */
	public String fun1(){
		System.out.println("fun1...");
		return NONE;
	}
	
	public String fun2(){
		System.out.println("fun2...");
		return NONE;
	}
	
}

struts.xml

<?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>
	
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	
	<package name="demo4" namespace="/" extends="struts-default">
	
		<!-- 訪問:http://localhost:8080/Struts1/action11.action ,將執行Action1的fun1() -->
		<action name="action11" class="zh.action.demo.Action1" method="fun1"></action>
		
		<!-- 訪問:http://localhost:8080/Struts1/action12.action ,將執行Action1的fun2() -->
		<action name="action12" class="zh.action.demo.Action1" method="fun2"></action>
		
	</package>

</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
 <!--  配置Struts的攔截器 -->
  <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>

2.萬用字元方式

struts.xml

<?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>
	
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	
	<package name="demo4" namespace="/" extends="struts-default">
		
		<!-- {1}匹配name屬性值中的第一個 * -->
		<!-- 訪問:http://localhost:8080/Struts1/action1_fun2.action,* 就是fun1 -->
		<!-- 訪問:http://localhost:8080/Struts1/action1_fun2.action,* 就是fun2 -->
		
		<action name="action1_*" class="zh.action.demo.Action1" method="{1}"></action>
		
	</package>

</struts>

3.動態訪問

首先要開啟“動態訪問”

<?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>
	
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	
	<!-- 開啟動態訪問 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

	<package name="demo4" namespace="/" extends="struts-default">
		
		<!-- 動態訪問寫法 http://localhost:8080/Struts1/action1!方法名稱.action -->
		<action name="action1" class="zh.action.demo.Action1"></action>
		
	</package>

</struts>