1. 程式人生 > >struts2使用通配符調用action

struts2使用通配符調用action

pub struts-2 1.0 local ace war odi xtend log

struts2配置文件代碼:

<?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.devMode" value="true" />
	<package name="student" namespace="/student" extends="struts-default">
		<action name="*_*" class="com.dbj.action.{1}Action" method="{2}">
			<result>/hello.jsp</result>
		</action>
		<action name="Student_add" class="com.dbj.action.StudentAction" method="add">
			<result>/index.jsp</result>
		</action>
	</package>
</struts>
Java後臺代碼:
public class StudentAction extends ActionSupport{

	public String add() {
		System.out.println("add");
		return SUCCESS;
	}
	
	public String update() {
		System.out.println("update");
		return SUCCESS;
	}
	
	public String search() {
		System.out.println("search");
		return SUCCESS;
	}
}

  瀏覽器URL:http://localhost:8080/01Struts2/student/Student_add.action

action name="*_*"中的第一個*匹配的是Student,第二個*匹配add;

class="com.dbj.action.{1}Action" method="{2}":{1}=Student,{2}=add;
調用結果返回hello.jsp;
如果配置文件中有一個action的名稱和Student_add完全匹配,則調用匹配最精確地action
如上述配置文件中則返回index.jsp;

struts2使用通配符調用action