歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2動態調用DMI及錯誤解決方法

Struts2動態調用DMI及錯誤解決方法

日期:2017/3/1 9:29:50   编辑:Linux編程

在Strust2中action可以定義自己的方法,調用方法有兩種方式,一種方式是struts.xml中指定method來表示需要用到的方法,但是這種方法缺點在於如果你的Action中有很多方法則要多次配置,這樣很繁瑣,另一種方法是使用動態方法調用DMI。

舉一個簡單的例子——

比如LoginAction中有add,dell方法,則

1、配置struts.xml文件

1 <package name="default" extends="struts-default">
2 <action name="login" class="com.action.loginAction">
3 <result name="add">/add.jsp</result>
4 <result name="dell">/dell.jsp</result>
5 </action>
6 </package>

2、在 loginAction中 編寫add方法和dell方法:

public String add()
{
return "add";
}
public String dell()
{
return "dell";
}

3、鏈接中使用!動態調用所需方法:

<a href="login!add">調用add方法</a>
<a href="login!dell">調用dell方法</a>


常見錯誤:

There is no Action mapped for namespace [/XXX] and action name [xxx!xxx] associated with context path [/xxx_method].

解決方法:

默認的Struts.xml中是這樣的

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

這樣我們在地址欄輸入感歎號動態調用方法的時候會出錯,我們只需更改這一句即可

<!-- 打開Struts的DMI -->

<constant name="struts.enable.DynamicMethodInvocation" value="true" />

struts2文件上傳(保存為BLOB格式) http://www.linuxidc.com/Linux/2014-06/102905.htm

Struts2的入門實例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2實現ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm

遇到的Struts2文件下載亂碼問題 http://www.linuxidc.com/Linux/2014-03/98990.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

Struts2 注解模式的幾個知識點 http://www.linuxidc.com/Linux/2013-06/85830.htm

Struts 的詳細介紹:請點這裡
Struts 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved