Action執(zhí)行的時候并不一定要執(zhí)行execute方法。
可以在配置文件中配置Action的時候用method=來指定執(zhí)行哪個方法也可以在url地址中動態(tài)指定(動態(tài)方法調(diào)用DMI)(推薦),前者會產(chǎn)生太多的action,所以不推薦使用。
struts2.xml中的配置:
<action name="hello" class="test.struts2.TestAction">
<result>/Hello.jsp</result>
<result name="add">/add.jsp</result> name表示方法返回的結(jié)果的字符串相對應(yīng)。
<result name="update">/update.jsp</result>
</action>
jsp頁面中的代碼:
<a href="test/hello">hello</a><br>
<a href="test/hello!add">hello!add</a><br> !表示代碼的動態(tài)調(diào)用,我要使用該action中的哪個方法。
<a href="test/hello!a">hello!update</a>
action中的代碼:
public class TestAction extends ActionSupport {
public String execute() {
return "success";
}
public String add() { //add() 對應(yīng)DMI的!號后的字符就是方法名
return "add"; //返回的add對應(yīng)struts2.xml中的result的name值,找到相對應(yīng)的結(jié)果。
}
public String update() {
return "update";
}
}