叶明的javablog

java学习天堂,个人关于其他blog:blog.javaworker.cn,欢迎大家访问
posts - 64, comments - 50, trackbacks - 0, articles - 3

Struts常见错误及原因[转载]

Posted on 2006-04-01 12:11 天空苍茫 阅读(233) 评论(0)  编辑  收藏

异常

 

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 工具来检查。

 

异常

 

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

 

可能原因

 

特别提示:因为有很多中情况会导致这个错误的发生,所以推荐大家调高你的 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.

 

异常

 

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 ”。

错误

 

java.lang.NoClassDefFoundError: org/apache/struts/action/ActionForm

 

可能原因

 

这个错误主要发生在在 classpath 中找不到相应的 Java .class 文件。如果这个错误发生在 web 应用程序的运行时,主要是因为指定的 class 文件不在 web server classpath 中( /WEB-INF/classes   /WEB-INF/lib)。

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

 

 

异常

 

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.

 

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

 

异常

 

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"/>

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。 

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

 

可能原因

 

很显然,这个错误是发生在使用资源文件时,而 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

 

错误

 

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

错误

 

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" 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.” 的错误信息。


只有注册用户登录后才能发表评论。


网站导航: