`
jandroid
  • 浏览: 1894391 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Struts常见错误及原因分析

 
阅读更多

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

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


Cannotretrievemappingforaction

异常
javax.servlet.jsp.JspException:Cannotretrievemappingforaction/Login(/Login是你的action名字)




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





Cannotretrievedefinitionforformbeannull

异常
org.apache.jasper.JasperException:Cannotretrievedefinitionforformbeannull

可能原因

这个异常是因为Struts根据struts-config.xml中的mapping没有找到action期望的formbean。大部分的情况可能是因为在form-bean中设置的name属性和action中设置的name属性不匹配所致。换句话说,action和form都应该各自有一个name属性,并且要精确匹配,包括大小写。这个错误当没有name属性和action关联时也会发生,如果没有在action中指定name属性,那么就没有name属性和action相关联。当然当action制作某些控制时,譬如根据参数值跳转到相应的jsp页面,而不是处理表单数据,这是就不用name属性,这也是action的使用方法之一。





Noactioninstanceforpath/xxxxcouldbecreated

异常
Noactioninstanceforpath/xxxxcouldbecreated

可能原因
特别提示:因为有很多中情况会导致这个错误的发生,所以推荐大家调高你的web服务器的日志/调试级别,这样可以从更多的信息中看到潜在的、在试图创建action类时发生的错误,这个action类你已经在struts-config.xml中设置了关联(即添加了<action>标签)。

在struts-config.xml中通过action标签的class属性指定的action类不能被找到有很多种原因,例如:

定位编译后的.class文件失败。Failuretoplacecompiled.classfilefortheactionintheclasspath(在web开发中,class的的位置在rWEB-INF/classes,所以你的actionclass必须要在这个目录下。例如你的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的问题。例如webserver没有发现你的资源文件,资源文件必须在WEB-INF/classes/目录下。

Probleminstruts-config.xmlfilewithactionmapping.

Problemwithdata-sources.xmlfile.

相关链接
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg65874.html
ActionMappingmistakeinstruts-config.xml:
http://www.manning.com/ao/readforum.html?forum=siaao&readthread=177
data-sources.xmlfile?:
http://www.caucho.com/quercus/faq/section.xtp?section_id=30





NogettermethodforpropertyXXXXofbeanorg.apache.struts.taglib.html.BEAN

异常
javax.servlet.jsp.JspException:Nogettermethodforpropertyusernameofbeanorg.apache.struts.taglib.html.BEAN

可能原因
没有位formbean中的某个变量定义getter方法

这个错误主要发生在表单提交的FormBean中,用struts标记<html:textproperty=”username”>时,在FormBean中必须有一个getUsername()方法。注意字母“U”。

RelatedLinks
Casecantripupthematchingbetweengetmethod'snameandnamespecifiedinStrutstag
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文件不在webserver的classpath中(/WEB-INF/classes和/WEB-INF/lib)。

在上面的错误中,原因是找不到ActionForm类。

ThiserrorissometimesseenwhenoneormoreActionForm.classinstancesareactuallyintheclasspath.ThismostoftenoccurswhenActionForm.classismadeavailablecorrectlybyplacingstruts.jarinthe/WEB-INF/libdirectory.WhenthislibraryhasbeencorrectlyplacedanditisverifiedthatActionForm.classactuallyispresentinthestruts.jarfile,theproblemiseitherthatmorethanonecopyofActionForm.classisintheclasspathor(morelikely)thatduplicateversionsofclassfilesotherthanActionFormareinthesameclasspath,causingconfusion.ThisisespeciallytrueifaclassthatextendsActionFormismadeavailabletwice,suchasinan.earfilethatencompassesa.warfileaswellasinthe.warfile'sownclasspath(/WEB-INF/classes).Thisproblemcanberesolvedbyguaranteeingthattherearenoredundantclasses,especiallythoserelatedtoStruts(directlyfromStrutsorextensionsofStruts),inthewebapplication'sview.

相关连接
EJBandWebSharedLinks:
http://forum.java.sun.com/thread.jsp?forum=26&thread=413060&tstart=0&trange=15
KeepActionandActionForm(andtheirchildren)asnon-overlappingunit(s)ofanapplication
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg47466.html
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg47467.html





Exceptioncreatingbeanofclassorg.apache.struts.action.ActionForm:{1}

异常
javax.servlet.jsp.JspException:Exceptioncreatingbeanofclassorg.apache.struts.action.ActionForm:{1}

可能原因
InstantiatingStruts-providedActionFormclassdirectlyinsteadofinstantiatingaclassderivedoffActionForm.Thismightoccurimplicitlyifyouspecifythataform-beanisthisStrutsActionFormclassratherthanspecifyingachildofthisclassfortheform-bean.

NotassociatinganActionForm-descendedclasswithanactioncanalsoleadtothiserror.

RelatedLinks






CannotfindActionMappingsorActionFormBeanscollection

Exception
javax.servlet.jsp.JspException:CannotfindActionMappingsorActionFormBeanscollection

可能原因
不是标识StrutsactionServlet的<servlet>标记就是映射.do扩展名的<sevlet-mapping>标记或者两者都没有在web.xml中声明。

在struts-config.xml中的打字或者拼写错误也可导致这个异常的发生。例如缺少一个标记的关闭符号/>。最好使用strutsconsole工具检查一下。

另外,load-on-startup必须在web.xml中声明,这要么是一个空标记,要么指定一个数值,这个数值用来表servlet运行的优先级,数值越大优先级越低。

还有一个和使用load-on-startup有关的是使用Struts预编译JSP文件时也可能导致这个异常。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics