Strus常见错误及原因分析

Strus常见错误及原因分析(ZZ)   [ 日期:2006-01-10 ]   [作者:hry23 来自:BlogJava ]

本篇文章包含了在用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.
相关链接

 

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中( /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.
相关连接

 

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文件时也可能导致这个异常。
相关链接

 

NullPointerException at ... RequestUtils.forwardURL
异常
java.lang.NullPointerException at org.apache.struts.util.RequestUtils.forwardURL(RequestUtils.java:1223)
可能原因
在struts-config.xml中的forward元素缺少path属性。例如应该是如下形式:
<forward name="userhome" path="/user/userhome.jsp"/>

 

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的值对没有在资源文件 ApplicationResources.properties中定义。如果你使用eclipse时经常碰到这样的情况,当项目重新编译时,eclipse会自动将classes目录下的资源文件删除。
资源文件ApplicationResources.properties 不在classpath中 应将资源文件放到 WEB-INF/classes 目录下,当然要在struts-config.xml中定义)

 

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 html:options tag instead of specifying the options in its body -- this assumes options are specified in ApplicationResources.properties file)
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 input attribute in action mapping in struts-config.xml file for the action with the name specified in the error message. An input attribute is not required if form validation is not performed (either because the validate attribute is set to false or because the validation method in the relevant form class is not implemented. The input attribute specifies the page leading to this action because that page is used to display error messages from the form validation.
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.”的错误信息。

 
00000


[本日志由 hry23 于 2006-01-10 04:59 PM 编辑]
   引用通告地址 (0):http://www.hry.cn/trackback.asp?tbID=359
收藏到:[ 新浪ViVi][ 365Key][ YouNote][ 搜狐网摘][ 博采中国][ 365Fav][ 天极网摘][ 和讯网摘]
评论:
 hry23 于 2006-03-06 05:20 PM 发表评论: (回复该评论) 
以下所说的struts-config.xml和ApplicationResources.properties等文件名是缺省时使用的,如果你使用了多模块,或指定了不同的资源文件名称,这些名字要做相应的修改。

1、“No bean found under attribute key XXX”
在struts-config.xml里定义了一个ActionForm,但type属性指定的类不存在,type属性的值应该是Form类的全名。或者是,在Action的定义中,name或attribute属性指定的ActionForm不存在。


2、“Cannot find bean XXX in any scope”
在Action里一般会request.setAttribute()一些对象,然后在转向的jsp文件里(用tag或request.getAttribute()方法)得到这些对象并显示出来。这个异常是说jsp要得到一个对象,但前面的Action里并没有将对象设置到request(也可以是session、servletContext)里。
可能是名字错了,请检查jsp里的tag的一般是name属性,或getAttribute()方法的参数值;或者是Action逻辑有问题没有执行setAttribute()方法就先转向了。
还有另外一个可能,纯粹是jsp文件的问题,例如<logic:iterate>会指定一个id值,然后在循环里<bean:write>使用这个值作为name的值,如果这两个值不同,也会出现此异常。(都是一个道理,request里没有对应的对象。)


3、“Missing message for key "XXX"”
缺少所需的资源,检查ApplicationResources.properties文件里是否有jsp文件里需要的资源,例如:


<bean:message key="msg.name.prompt"/>

这行代码会找msg.name.prompt资源,如果AppliationResources.properties里没有这个资源就会出现本异常。在使用多模块时,要注意在模块的struts-config-xxx.xml里指定要使用的资源文件名称,否则当然什么资源也找不到,这也是一个很容易犯的错误。

4、“No getter method for property XXX of bean teacher”
这条异常信息说得很明白,jsp里要取一个bean的属性出来,但这个bean并没有这个属性。你应该检查jsp中某个标签的property属性的值。例如下面代码中的cade应该改为code才对:

<bean:write name="teacher" property="cade" filter="true"/>


5、“Cannot find ActionMappings or ActionFormBeans collection”
待解决。

6、“Cannot retrieve mapping for action XXX”
在.jsp的<form>标签里指定action='/XXX',但这个Action并未在struts-config.xml里设置过。

7、HTTP Status 404 - /xxx/xxx.jsp
Forward的path属性指向的jsp页面不存在,请检查路径和模块,对于同一模块中的Action转向,path中不应包含模块名;模块间转向,记住使用contextRelative="true"。

8、没有任何异常信息,显示空白页面
可能是Action里使用的forward与struts-config.xml里定义的forward名称不匹配。


9、“The element type "XXX" must be terminated by the matching end-tag "XXX".”
这个是struts-config.xml文件的格式错误,仔细检查它是否是良构的xml文件,关于xml文件的格式这里就不赘述了。

10、“Servlet.init() for servlet action threw exception”
一般出现这种异常在后面会显示一个关于ActionServlet的异常堆栈信息,其中指出了异常具体出现在代码的哪一行。我曾经遇到的一次提示如下:

java.lang.NullPointerException
    at org.apache.struts.action.ActionServlet.parseModuleConfigFile(ActionServlet.java:1003)
    at org.apache.struts.action.ActionServlet.initModuleConfig(ActionServlet.java:955)


为解决问题,先下载struts的源码包,然后在ActionServlet.java的第1003行插入断点,并对各变量进行监视。很丢人,我竟然把struts-config.xml文件弄丢了,因此出现了上面的异常,应该是和CVS同步时不小心删除的。

11、“Resources not defined for Validator”
这个是利用Validator插件做验证时可能出现的异常,这时你要检查validation.xml文件,看里面使用的资源是否确实有定义,form的名称是否正确,等等。

上面这些是我在用Struts做项目时遇到过的问题,其中一些曾困绕我不少时间,其实大部分都是自己不细心造成的。希望这篇文章能对你的开发有所帮助,并欢迎继续补充。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值