1. 程式人生 > >(action動態方法呼叫)struts通過ActionName!methodName.action,即通過感嘆號(即!)訪問某Action的方法,確老是報錯

(action動態方法呼叫)struts通過ActionName!methodName.action,即通過感嘆號(即!)訪問某Action的方法,確老是報錯

(struts通過ActionName!methodName.action,即通過感嘆號(即!)訪問某Action的方法,如下所示:

http url連結如下:

http://127.0.0.1:8080/News/newsAction!findAll.action

上面連結中News是工程名,newsAction是類名,findAll是newsAction類中的方法。

NewsAction.java如下

public class NewsAction extends BaseAction implements ModelDriven<News>{
private NewsService newsService = null;
private News model = new News();
public String findAll() throws IOException{
List<News> news = newsService.findAll();
JSONArray array = JSONArray.fromObject(news);
this.getResponse().setContentType("text/html; charset=utf-8");
this.getResponse().setHeader("Cache-Control", "no-cache");
this.getResponse().getWriter().print(array);
return null;
}

}

struts.xml配置如下:

<package name="default" extends="struts-default">
<action name="newsAction" class="newsAction">

</action>
</package>

spring.xml配置如下:

<bean id="newsAction" class="com.hhl.news.action.NewsAction">
<property name="newsService" ref="newsService"/>
</bean>

卻老是報錯,訪問不上,報錯如下:

There is no Action mapped for namespace [/] and action name [newsAction!findAll] associated with context path [/News].

namespace啥的也沒有配錯,什麼都沒錯。為什麼?????

答案是:將Struts-core.jar包中的default.properties檔案中,struts.enable.DynamicMethodInvocation屬性設定為true!!Struts-core.jar包中該屬性預設竟然是false的!

詳細解釋:使用動態方法呼叫必須設定

Struts2允許動態方法呼叫。開啟系統的動態方法呼叫是通過設定struts.enable.DynamicMethodInvocation常量完成的,設定該常量的值為true,將開啟動態方法呼叫;否則將關閉關閉動態方法呼叫!!

但那個default.properties是不能被更改的,有如下解決方法:

方法1:。如需要更改裡面的配置資訊,可以在src根目錄下建一個 struts.properties的配置檔案,然後重寫載入所要更改的配置資訊。http://blog.csdn.net/wfcaven/article/details/5937557

方法2:

在struts.xml里加入    <constant name="struts.enable.DynamicMethodInvocation" value="true" />