本篇文章包含了在用Struts開發web應用時經常碰到的一些異常和錯誤,根據異常或錯誤信息本身,經常可以找到潛在的錯誤發生原因。
下面列出了一些Struts的常見錯誤和異常,並給出了一些可能發生此類錯誤或異常的原因。有的後面有相關連接,你可以通過它找到更多的信息。
Cannot retrieve mapping for action
|
|
異常
|
javax.servlet.jsp.JspException: Cannot retrieve mapping for action /Login (/Login是你的action名字)
|
|
|
可能原因
|
action沒有再
|
Cannot retrieve definition for form bean null
|
|
異常
|
org.apache.jasper.JasperException: Cannot retrieve definition for form bean null
|
可能原因
|
這個異常是因爲Struts根據struts-config.xml中的mapping沒有找到action期望的form bean。大部分的情況可能是因爲在form-bean中設置的name屬性和action中設置的name屬性不匹配所致。換句話說,action和form都應該各自有一個name屬性,並且要精確匹配,包括大小寫。這個錯誤當沒有name屬性和action關聯時也會發生,如果沒有在action中指定name屬性,那麼就沒有name屬性和action相關聯。當然當action製作某些控制時,譬如根據參數值跳轉到相應的jsp頁面,而不是處理表單數據,這是就不用name屬性,這也是action的使用方法之一。
|
No action instance for path /xxxx could be created
|
|
異常
|
No action instance for path /xxxx could be created
|
可能原因
|
特別提示:因爲有很多中情況會導致這個錯誤的發生,所以推薦大家調高你的web服務器的日誌/調試級別,這樣可以從更多的信息中看到潛在的、在試圖創建action類時發生的錯誤,這個action類你已經在struts-config.xml中設置了關聯(即添加了<action>標籤)。
|
在
|
|
在
|
|
你的classpath的問題。例如web server沒有發現你的資源文件,資源文件必須在
|
|
Problem in
|
|
Problem with
|
|
相關鏈接
|
|
No getter method for property XXXX of bean org.apache.struts.taglib.html.BEAN
|
|
異常
|
javax.servlet.jsp.JspException: No getter method for property username of bean org.apache.struts.taglib.html.BEAN
|
可能原因
|
沒有位form bean中的某個變量定義getter 方法
|
這個錯誤主要發生在表單提交的FormBean中,用struts標記<html:text property=”username”>時,在FormBean中必須有一個getUsername()方法。注意字母“U”。
|
|
Related Links
|
|
java.lang.NoClassDefFoundError: org/apache/struts/action/ActionForm
|
|
錯誤
|
java.lang.NoClassDefFoundError: org/apache/struts/action/ActionForm
|
可能原因
|
這個錯誤主要發生在在classpath中找不到相應的Java .class文件。如果這個錯誤發生在web應用程序的運行時,主要是因爲指定的class文件不在web server的classpath中(
|
This error is sometimes seen when one or more
|
|
相關連接
|
|
Exception creating bean of class org.apache.struts.action.ActionForm: {1}
|
|
異常
|
javax.servlet.jsp.JspException: Exception creating bean of class org.apache.struts.action.ActionForm: {1}
|
可能原因
|
Instantiating Struts-provided
|
Not associating an
|
|
Related Links
|
|
Cannot find ActionMappings or ActionFormBeans collection
|
|
Exception
|
javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection
|
可能原因
|
不是標識Struts actionServlet的<servlet>標記就是映射.do擴展名的<sevlet-mapping>標記或者兩者都沒有在web.xml中聲明。
|
在struts-config.xml中的打字或者拼寫錯誤也可導致這個異常的發生。例如缺少一個標記的關閉符號/>。最好使用struts console工具檢查一下。
|
|
另外,load-on-startup必須在web.xml中聲明,這要麼是一個空標記,要麼指定一個數值,這個數值用來表servlet運行的優先級,數值越大優先級越低。
|
|
還有一個和使用load-on-startup有關的是使用Struts預編譯JSP文件時也可能導致這個異常。
|
|
相關鏈接
|
|
NullPointerException at ... RequestUtils.forwardURL
|
|
異常
|
java.lang.NullPointerException at org.apache.struts.util.RequestUtils.forwardURL(RequestUtils.java:1223)
|
可能原因
|
在struts-config.xml中的forward元素缺少path屬性。例如應該是如下形式:
|
Cannot find bean org.apache.struts.taglib.html.BEAN in any scope
|
|
Exception
|
javax.servlet.jsp.JspException: Cannot find bean org.apache.struts.taglib.html.BEAN in any scope
|
|
|
Probable Causes
|
試圖在Struts的form標記外使用form的子元素。這常常發生在你在</html:form>後面使用Struts的html標記。 另外要注意可能你不經意使用的無主體的標記,如<html:form … />,這樣web 服務器解析時就當作一個無主體的標記,隨後使用的所有<html>標記都被認爲是在這個標記之外的,如又使用了<html:text property=”id”> 還有就是在使用taglib引入HTML標記庫時,你使用的prefix的值不是html。 |
相關連接
|
|
Missing message for key xx.xx.xx
|
|
Exception
|
javax.servlet.jsp.JspException: Missing message for key xx.xx.xx
|
Probable Causes
|
這個key的值對沒有在資源文件
|
|
Cannot find message resources under key org.apache.struts.action.MESSAGE
|
|
異常
|
Cannot find message resources under key org.apache.struts.action.MESSAGE
|
可能原因
|
很顯然,這個錯誤是發生在使用資源文件時,而Struts沒有找到資源文件。
|
Implicitly trying to use message resources that are not available (such as using empty
|
|
XML parser issues -- too many, too few, incorrect/incompatible versions
|
|
Related Links
|
|
No input attribute for mapping path /loginAction
|
|
錯誤
|
No input attribute for mapping path /xxxxAction
|
可能原因e
|
No
|
Related Links
|
|
Strange Output Characters
|
|
錯誤
|
Strange and seemingly random characters in HTML and on screen, but not in original JSP or servlet.
|
可能原因
|
混和使用Struts的html:form標記和標準的HTML標記不正確。
|
使用的編碼樣式在本頁中不支持。
|
"Document contained no data" or no data rendered on page
|
|
錯誤
|
"Document contained no data" in Netscape
|
No data rendered (completely empty) page in Microsoft Internet Explorer
|
|
可能原因
|
使用一個Action的派生類而沒有實現perform()方法或execute()方法。在Struts1.0中實現的是perform()方法,在Struts1.1中實現的是execute()方法,但Struts1.1向後兼容perform()方法。 但你使用Struts1.1創建一個Action的派生類,並且實現了execute()方法,而你在Struts1.0中運行的話,就會得到"Document contained no data" error message in Netscape or a completely empty (no HTML whatsoever) page rendered in Microsoft Internet Explorer.”的錯誤信息。 |