导读:
1、
org.springframework.orm.ObjectRetrievalFailureException: Object of class [com.xindeco.myregister.pojo.MyRegisterInfo] with identifier [14]: not found
MyRegisterInfo在数据库没有identifier [14]对象。
2、
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.xindeco.myregister.pojo.MyRegisterInfo column: password (should be mapped with insert="false" update="false")
出错原因:password 和repassword同时对应数据库表中的password一列,同时update和insert都设为true。
xml文件如下:
type="java.lang.String"
update="false"
insert="false"
access="property"
column="password"
length = "32"
/> type="java.lang.String"
update="false"
insert="false"
access="property"
column="password"
length = "32"
/>
解决的方法:
将repassword的insert和update设为false。
3、
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed;
nested exception is org.hibernate.PropertyNotFoundException: Could not find a getter for ID in class
错误原因:hibernate的映射文件中ID是大写的,而pojo的类中id是小写的
注意事项:每个pojo的类都要继承abstractEntity,其中abstractEntity类有个ID的属性要重写
public abstract class AbstractEntity
implements Entity, BaseDTO {
abstract public long getID();
abstract public void setID(long id);
public int hashCode() {
return (int)this.getID();
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Entity) {
return this.getID() == ( (Entity) obj).getID();
}
return false;
}
}
4、
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
错误原因:
在application.xml文件中deleteRegister方法以delete开头,并没有被设定为可更改的,应如下设置:
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
加上一行
PROPAGATION_REQUIRED
5、
ERROR org.apache.struts.util.RequestUtils - Error creating form bean of class com.xindeco.business.web.form.GraAppInfoForm
public class GraAppInfoForm
extends ActionForm 错误写成
public abstratic class GraAppInfoForm
extends ActionForm
6、
2006-04-25 08:56:38,495 ERROR com.xindeco.business.web.action.GraAppAction - com.xindeco.business.web.action.GraAppAction@8e2fb5
java.lang.ClassCastException: $Proxy0
at com.xindeco.business.web.action.GraAppAction.newone(GraAppAction.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
at com.xindeco.core.web.action.BaseAction.dispatchMethod(BaseAction.java:153)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
actioin类中的newone方法如下:
public class GraAppAction
extends BaseAction {
public ActionForward newone(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
GraAppService graservice = (GraAppService) this.getBean("GraAppService");
}
applicationcontext中的GraAppService的配置如下:
因此this.getBean("GraAppService");是为了得到GraAppServicImpl类的实现。GraAppService是interface
public class GraAppServiceImpl extends BaseServiceImpl
implements GraAppService{
}
7、org.hibernate.hql.ast.QuerySyntaxException: Demand is not mapped. [from Demand where unitid = ? and needNum >usedNeedNum]
hibernate.cfg.xml没有配置Demand.hbm.xml文件的目录
8、org.springframework.jdbc.BadSqlGrammarException: Bad SQL grammar [] in task 'Hibernate operation'; nested exception is java.sql.SQLException: 列名 'id' 无效。
java.sql.SQLException: 列名 'name' 无效。
因为hibernate声明的id,name的columnid属性没有与数据库的字段对应,所以id,name无效。
9、java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:415)
at java.lang.Integer.parseInt(Integer.java:497)
at com.xindeco.business.service.impl.DemandServiceImpl.findDemandListByUnitId(DemandServiceImpl.java:33)
错误语句为
needNum = Integer.parseInt(demand.getNeedNum());
usedNeedNum = Integer.parseInt(demand.getUsedNeedNum());
因为demand.getUsedNeedNum()==null,无法转化为string 类型,
10、rg.apache.jasper.JasperException: /GraAppInfo/GraAppInfoNew.jsp(343,29) According to TLD, tag bean:write must be empty, but is not
错误的原因:
private boolean one = false;
private boolean two = false;
private boolean three = false;
One
Two
Three
如果选中后被提交则相应的属性的值为true。
回页首
html:radio
html:radio标签生成一个radio。主要的用法有两种,下面我们通过代码来示例。
下面的代码示例了html:radio标签的一般用法,如果被提交则选中的radio的value值将被提交到radioForm中的id中。
One
Two
下面的代码示例了html:radio标签的典型用法,其中的persons和bean:define标签中的一致,您可以参考bean:define标签。我只介绍这个
,idName指定html:radio要使用的bean(这里为person),value="id"表示person的id属性将作为radio元素的value值而property="id"表示提交时选中的radio的值将被提交给radioForm中的id属性。
error=error with none value . error1=error1 with one value is {0} . error2=error2 with two values are {0} , {1} . error3=error3 with three values are {0} , {1} , {2} . error4=error4 with four values are {0} , {1} , {2} ,{3} . 下面的代码示例了actionErrors的构造: public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors actionErrors = new ActionErrors(); actionErrors.add("property1", new ActionMessage("error")); actionErrors.add("property2", new ActionMessage("error1","value0")); actionErrors.add("property2",new ActionMessage("error2","value0","value1")); actionErrors.add("property3",new ActionMessage("error3","value0","value1","value2")); actionErrors.add("property3",new ActionMessage("error4","value0","value1","value2","value3")); actionErrors.add("property4", new ActionMessage("error1",new Object[]{"value0"})); actionErrors.add("property4", new ActionMessage("error2",new Object[]{"value0","value1"})); actionErrors.add("property4", new ActionMessage("error3",new Object[]{"value0","value1", "value2"})); actionErrors.add("property5",new ActionMessage("error4",new Object[]{"value0","value1", "value2","value3"})); actionErrors.add("notBundle",new ActionMessage("not a bundle key",false)); return actionErrors; } errors标签代码示例:
logic:notMatch标签的应用正好和logic:match标签相反。
回页首
logic:equal
这里要介绍的不只是logic:equal(=)标签,而是要介绍一类标签,这类标签完成比较运算,包括:
logic:equal(=)
logic:notEqual(!=)
logic:greaterEqual(>=)
logic:lessEqual(<=)
logic:graterThan(>)
logic:lessThan(<)
该类标签的用法类似,我们只介绍logic:equal标签,其它的留给您自己去完成。
logic:equal是用来判断是否相等的。如果相等,该标签体中嵌入的内容就会被处理。该标签用于以下情况:
比较由该标签的cookie属性指定的cookie的值是否与该标签的value属性值相等。
比较由该标签的header属性指定的header的值是否与该标签的value属性值相等。
比较由该标签的name属性指定的JSP Bean是否与该标签的value属性值相等(property属性不出现)或比较由该标签的name属性指定的JSP Bean中的property属性值是否与该标签的value属性值相等。
比较由该标签的parameter属性指定的参数值(request中)是否与该标签的value属性值相等。
回页首
logic:forward
我觉得将forward和redirect这两个动作放到一起对比着介绍更加有利于理解,基于此原因也就将logic:forward和logic:redirect这两个标签也拿到这里一起介绍了。
让我们看看这两个动作的区别:
forward是在servlet内部执行,浏览器完全不会感知到这个动作,原始的url也不会改变,浏览器重新装载的话也只是对原始的请求进行简单的重复。
redirect则分成两个步骤:第一步是web应用程序告诉浏览器第二个url,然后浏览器向第二个url发送请求。
redirect比forward慢,因为浏览器要做二次请求。还有就是要注意,在第一次的请求作用域(request作用域)内的bean对于第二次请求是不可见的。
理解了上面描述的区别也就知道了什么时候该选用logic:forward标签什么时候该选用logic:redirect标签了。logic:forward标签完成PageContext.forward()或HttpServletResponse.sendRedirect(),如何选择由控制器决定。logic:redirect标签完成HttpServletResponse.sendRedirect()。
在使用logic:redirect标签时我们可以向使用html:link一样构造baseurl和query参数。如果您感兴趣可以参考html:link标签。
回页首
总结
这篇指南的背景是Struts-1.2.9,其中的所有的代码示例也都是在这个版本下调试通过的。目前Struts仍然在快速的发展中,但是从开发者应用的角度来讲还是比较稳定的。因此我有理由相信其中的代码示例也可以很好的运行在其它的版本之上。最后希望这篇指南能够让您心情愉快!
参考资料
图解 Tomcat 体系结构
使用Log4j进行日志记录
Struts的官方文档
Struts的用户指南
cookie spec,这是关于Cookie最的全面的说明。
HTML元素
关于作者
solo L 一位有些理想主义的软件工程师,创建了solol.org。他常常在这里发表一些对技术的见解。
Copyright ? SoloL.org
12:43 | 永久链接 | 浏览 (37) | 评论 (0) | 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文件中使用
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中设置了关联(即添加了
在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标记
时,在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的
标记就是映射.do扩展名的
标记或者两者都没有在web.xml中声明。
在struts-config.xml中的打字或者拼写错误也可导致这个异常的发生。例如缺少一个标记的关闭符号/>。最好使用struts console工具检查一下。
另外,load-on-startup必须在web.xml中声明,这要么是一个空标记,要么指定一个数值,这个数值用来表servlet运行的优先级,数值越大优先级越低。
还有一个和使用load-on-startup有关的是使用Struts预编译JSP文件时也可能导致这个异常。
相关链接
Explicitly Define
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=50&t=001055
http://threebit.net/tutorials/ejb/general/
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的子元素。这常常发生在你在
后面使用Struts的html标记。
另外要注意可能你不经意使用的无主体的标记,如
,这样web 服务器解析时就当作一个无主体的标记,随后使用的所有标记都被认为是在这个标记之外的,如又使用了
还有就是在使用taglib引入HTML标记库时,你使用的prefix的值不是html。
相关连接
Using form subelements outside of a form tag
http://forum.java.sun.com/thread.jsp?thread=337537&forum=4&message=1384153
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
Provide Struts with Resource Bundle
http://threebit.net/tutorials/ejb/general/
XML Parser Issues
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg15779.html
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.”的错误信息。
12:42 | 永久链接 | 浏览 (88) | 评论 (0) | struts |
Struts提供了五个标签库,即:HTML、Bean、Logic、Template和Nested。
标签库
说明
HTML 标签 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单
Bean 标签 在访问JavaBeans 及其属性,以及定义一个新的bean 时使用
Logic 标签 管理条件产生的输出和对象集产生的循环
Template 标签 随着Tiles框架包的出现,此标记已开始减少使用
Nested 标签 增强对其他的Struts 标签的嵌套使用的能力
标签的公共特征
使用固定属性名称的Struts 标签:
属性
说明
id 命名自定义标签创建时的脚本变量名。
name 指出关键字值,在该关键字下可以找到一个存在的bean 。如果给出了scope属性,则仅仅在scope中查找。否则,根据标准的顺序在各种scope中查找:(page, request, session, or application)。
property 指出bean 中的某个属性,可以在其中检索值。如果没有标明,则使用对象本身的值。
scope 定义了Bean在哪个范围(page, request, session, or application)中被查找。如果没有标明按顺序查找。脚本变量(见id)将在相同的范围中创建。
Struts 标签也支持嵌套引用,例如:
Property="foo.bar.baz"
这相当于进行下面的调用:
getFoo().getBar().getBaz();
或者做为setter:
getFoo().getBar().setBaz(value);
虽然Struts 标签的设计原意是为了避免使用scriptlet,scriptlet的表达式还能够提供给所有的Struts 标签使用。但请确保使用完整的表达式:
错误: