Struts常见错误及原因分析

本篇文章包含了在用Struts开发web应用时经常碰到的一些异常和错误,根据异常或错误信息本身,经常可以找到潜在的错误发生原因。

下面列出了一些Struts的常见错误和异常,并给出了一些可能发生此类错误或异常的原因。有的后面有相关连接,你可以通过它找到更多的信息。


Cannot retrieve mapping for action 
 
异常 
 javax.servlet.jsp.JspException: Cannot retrieve mapping for action /Login ( /Login 是你的 action 名字) 
 
 
  
 
可能原因 
 action 没有再 struts-config.xml 中定义,或没有找到匹配的 action ,例如在 JSP 文件中使用 <html:form action=" Login.do " . 将表单提交给 Login.do 处理,如果出现上述异常,请查看 struts-config.xml 中的定义部分,有时可能是打错了字符或者是某些不符合规则,可以使用 struts console 工具来检查。 
 

 


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> 标签)。 
 
在 struts-config.xml 中通过action标签的class属性指定的action类不能被找到有很多种原因,例如: 

定位编译后的 .class 文件失败。 Failure to place compiled .class file for the action in the classpath ( 在 web 开发中, class 的的位置在 r WEB-INF/classes, 所以你的action class必须要在这个目录下。例如你的action类位于WEB-INF/classes/action/Login.class,那么在struts-config.xml中设置action的属性type时就是action.Login ). 
拼写错误,这个也时有发生,并且不易找到,特别注意第一个字母的大小写和包的名称。 
 
在 struts-config.xml中指定的action类没有继承自Stuts的Action类,或者你自定义的Action类没有继承自Struts提供的Action类。 

你的action类必须继承自Struts提供的Action类。 
 
你的classpath的问题。例如web server没有发现你的资源文件,资源文件必须在 WEB-INF/classes/目录下 。 
 
Problem in struts-config.xml file with action mapping. 
 
Problem with data-sources.xml file. 
 
相关链接 
 http://www.mail-archive.com/struts-user@jakarta.apache.org/msg65874.html 
Action Mapping mistake in struts-config.xml :
http://www.manning.com/ao/readforum.html?forum=siaao&readthread=177 
data-sources.xml file?:
http://www.caucho.com/quercus/faq/section.xtp?section_id=30 
 

 


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 
 Case can trip up the matching between get method's name and name specified in Struts tag
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=58&t=000163 
 

 


java.lang.NoClassDefFoundError: org/apache/struts/action/ActionForm 
 
错误 
 java.lang.NoClassDefFoundError: org/apache/struts/action/ActionForm 
 
可能原因 
 这个错误主要发生在在 classpath 中找不到相应的 Java .class 文件。如果这个错误发生在 web 应用程序的运行时,主要是因为指定的 class 文件不在 web server 的 classpath 中( /WEB-INF/classes 和 /WEB-INF/lib)。 

在上面的错误中,原因是找不到ActionForm类。 
 
This error is sometimes seen when one or more ActionForm.class instances are actually in the classpath. This most often occurs when ActionForm.class is made available correctly by placing struts.jar in the /WEB-INF/lib directory. When this library has been correctly placed and it is verified that ActionForm.class actually is present in the struts.jar file, the problem is either that more than one copy of ActionForm.class is in the classpath or (more likely) that duplicate versions of class files other than ActionForm are in the same classpath, causing confusion. This is especially true if a class that extends ActionForm is made available twice, such as in an .ear file that encompasses a .war file as well as in the .war file's own classpath ( /WEB-INF/classes ). This problem can be resolved by guaranteeing that there are no redundant classes, especially those related to Struts (directly from Struts or extensions of Struts), in the web application's view. 
 
相关连接 
 EJB and Web Shared Links:
http://forum.java.sun.com/thread.jsp?forum=26&thread=413060&tstart=0&trange=15 
Keep Action and ActionForm (and their children) as non-overlapping unit(s) of an application
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg47466.html
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg47467.html 
 

 


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 ActionForm class directly instead of instantiating a class derived off ActionForm . This might occur implicitly if you specify that a form-bean is this Struts ActionForm class rather than specifying a child of this class for the form-bean. 
 
Not associating an ActionForm -descended class with an action can also lead to this error. 
 
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 文件时也可能导致这个异常。 
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值