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文件中的配置(

<!-- 修改後綴 --> 

那麼有一個疑問,我們怎麼訪問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") }) 

   。。。。。。。

配置文件只需要配置註解即可:

<mvc:annotation-driven /> 
<context:annotation-config></context:annotation-config>                             不能簡寫成<context:annotation-config/>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章