Strus常见错误及原因分析
本篇文章包含了在用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 4月25号碰到这个错误 | |
异常 | 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的使用方法之一。 只需在struts-config.xml中的action中添加name属性,name完全对应着form-bean的名字 |
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.”的错误信息。 |