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;