Struts2 頁面url請求怎么找action
1.我們使用最原始的方法去查找action,不同注解。
struts.xml文件先配置
<!-- 新聞信息action -->
<action name="newsInfoAction" class="com.xxx.NewsInfoAction">
<result name="add">news/addNewsInfo.jsp</result>
<result name="update">news/editNewsInfo.jsp</result>
<result name="dataList">news/newsInfo.jsp</result>
</action>
action 默認執行的是NewsInfoAction中的excute方法。 http://localhost:8080/test/newsInfoAction.html 或者http://localhost:8080/test/newsInfoAction.action 看你如何在struts.xml文件中的配置(
<!-- 修改后綴 -->
<constant name="struts.action.extension" value="action,html" /> )
那么有一個疑問,我們怎么訪問NewsInfoAction中的其他方法呢?
訪問指定方法
方式一:
http://localhost/struts2/simple/hello!say.action
可以調用hello這個action中的say方法
方式二:
http://localhost/struts2/simple/hello.action?method:say=xxx
可以調用say方法,在這里,參數的名稱是:method:say,這是最主要的,struts2正是
根據參數的名稱來決定該調用哪個方法,而不是參數的值,所以參數的值可以是任意的
方式三:
struts2的配置文件的action標簽中存在一個method屬性,用來指定訪問特定的方法
<action name="hello" class="helloAction" method="say">
方式四:
<action name="hello_*" class="helloAction" method="{1}">
這樣在頁面中的action路徑可寫為hello_say.action就是訪問say方法了。
2.如果struts2已經交給spring容器管理了,我們就可以通過注解來找action以及該action的方法了。(推薦使用這種方法,這樣我們你就不用在struts.xml文件中再去配置各種action,可以給struts.xml減肥啦。)
url為 :http://localhost:8080/test/admin/editproduct.html?productInfoId=1 //參數可有可無
@SuppressWarnings("unchecked")
@Action(value = "/admin/editproduct", results = { @Result(name = "update", location = "product/editProductInfo.jsp") })
public String toUpdateProductInfo() throws Exception {
。。。。。。。
}
配置文件只需要配置注解即可:
<mvc:annotation-driven />
<context:annotation-config></context:annotation-config> 不能簡寫成<context:annotation-config/>