访问Servlet API
Struts2不提供Servlet API,Struts2提供了三种方式去访问Servlet AP。
1. ActionContext
2. 实现***Aware接口
3. ServletActionContext
Action搜索顺序
http://localhost:8080/struts2/
path1/
path2
/path3/student.action
第一步:判断package是否存在,如 :
path1/
path2
/path3
第二步:如果package存在,判断action是否存在,如果不存在,则去默认namespace的package里面寻找action,如果没有,则报错。
第二步:如果package不存在,检查上一级路径的package是否存在(直到默认namespace),重复第一步,如果没有,则报错。
动态方法调用
动态方法调用就是为了解决一个Action对应多个请求的处理,以免Action太多。
指定method属性
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>
<package name="default" namespace="/" extends="struts-default">
<action name="helloworld" class="com.lijy.action.HelloAction">
<result>/reult.jsp</result>
</action>
<!-- 指定method属性 ,即一个方法对应一个action
http://localhost:8080/HelloStruts2_1/addAction.action-->
<action name="addAction" method="add" class="com.lijy.action.HelloAction">
<result>/add.jsp</result>
</action>
<action name="updateAction" method="update" class="com.lijy.action.HelloAction">
<result>/update.jsp</result>
</action>
</package>
</struts>
在HelloAction需要增加与method同名的两个方法
package com.lijy.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
//1、指定methode属性
public String add() {
return SUCCESS;
}
public String update() {
return SUCCESS;
}
@Override
public String execute() throws Exception {
System.out.println("执行Action");
return SUCCESS;
}
}
感叹号方式
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>
<package name="default" namespace="/" extends="struts-default">
<action name="helloworld" class="com.lijy.action.{1}Action">
<result>/reult.jsp</result>
<!-- 2、感叹号方式,官方不推荐
需要配置struts.enable.DynamicMethodInvocation为true
http://localhost:8080/HelloStruts2_1/helloworld!add2.action
-->
<result name="add2">/add2.jsp</result>
<result name="update2">/update2.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>
HelloAction增加的方法为
//2、感叹号方式
public String add2() {
return "add2";
}
public String update2() {
return "update2";
}
通配符方式
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>
<package name="default" namespace="/" extends="struts-default">
<!-- 3、通配符方式,官方推荐,struts.enable.DynamicMethodInvocation改为false
在action属性中增加method="{1}",同时name值改为helloworld_*
访问为:http://localhost:8080/HelloStruts2_1/helloworld_update3.action
*匹配{1},如果 helloworld_*_*,method="{1}{2}"
如果name="*_*" method="{2}" class=com.lijy.action.{1}Action
第一个*匹配{1},第二个*匹配方法名
访问为http://localhost:8080/HelloStruts2_1/Hello_update3.action
也可以通配包名如class=class="com.lijy.{3}.action.{1}Action
-->
<action name="helloworld_*" method="{1}" class="com.lijy.action.HelloAction">
<result>/reult.jsp</result>
<!-- 3、通配符方式 http://localhost:8080/HelloStruts2_1/helloworld_add3.action -->
<result name="add3">/{1}.jsp</result>
<result name="update3">/{1}.jsp</result>
</action>
</package>
</struts>
HelloAction新增的方法
//3、通配符方式
public String add3() {
return "add3";
}
public String update3() {
return "update3";
}
第二种和第三种方式可以解决action配置过多的问题