1. 程式人生 > >No configuration found for the specified action解決辦法

No configuration found for the specified action解決辦法

使用Struts2,配置一切正常,使用常用tag也正常,但是在使用<s:form>標記時,發現控制檯總是輸出警告資訊, 

警告資訊內容如下:

警告: No configuration found for the specified action: 'ShowMessage' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
2008-9-6 11:35:47 org.apache.struts2.components.Form evaluateExtraParamsServletRequest
警告: No

configuration found for the specified action: 'ShowMessage' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

showmsg.jsp程式碼如下

<%@ taglib prefix="s" uri="/struts-tags" %>
......
<div>
hello world
<s:form name="ShowMessage" method="post" action="/hello/ShowMessage.action
" >
<s:textfield label="user name" name="username"></s:textfield>
<s:submit></s:submit>
</s:form>
</div>
......

struts.xml配置如下:
......
<struts>
<package name="hello" extends="struts-default" namespace="/hello">
<action name="ShowMessage" class="com.historycreator.strutstest.ShowMessage">
<result>/digg/showmsg.jsp</result>
</action
>
</package>
</struts>

開啟showmsg.jsp頁面時,就出現上述異常。嘗試了各種配置和訪問方法,均出現警告。搜尋網路給出的答案几乎都說把<s:form name="ShowMessage" method="post" action="/hello/ShowMessage.action" >中的.action去掉就可以解決了,做了測試,問題仍然沒有解決。更有人讓把<s:去掉,不用tag,直接寫<form>這個當然不會有問題,但是一會用標記,一會不用,很雜亂,並且不是解決問題的根本之道。

解決方法:
經過測試發現,是沒有正確使用tag的原因,這種情況下,正確的寫法應該是,<s:form name="ShowMessage" method="post" action="ShowMessage" namespace="/hello" >


原因分析:

因為開始使用的struts2標籤(form)並未指定namespace屬性。所以struts2會預設從根名稱空間"/" 搜尋action '/hello/ShowMessage.action',如搜尋不到則進入預設名稱空間''搜尋action串,在預設名稱空間中是肯定找不到自己定義的action的,所以,struts2丟擲一個警告資訊。

現在我們指定了namespace為/hello,則struts2會直接在/hello名稱空間尋找。可以想象,如果在這麼名稱空間裡找不到請求的action,也會丟擲一條類似的資訊。

注意,<s:form name="ShowMessage" method="post" action="ShowMessage" namespace="/hello" >
不能寫成<s:form name="ShowMessage" method="post" action="ShowMessage.action" namespace="/hello" >

這樣仍然有對應的警告資訊,並且提交後出現無法訪問的結果。因為沒有ShowMessage.action這樣一個action,這個.action不能由我們手工新增,Struts2會自動為你完成這個工作,並且手工新增是不行的,就不必多此一舉了。但是在其他的場合,比如使用超級鏈結,則可以加上這個.action