服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

一些错误地解决方法


1.如果使用ant和junit是被报找不到test,检查一下是否误用了private
出错信息:
    <failure message="no tests found in test.alltest" type="junit.framework.assertionfailederror">junit.framework.assertionfailederror: no tests found in test.alltest
该错误有个很特别的特点,当你不通过ant来运行测试,而是通过alltest类来运行的话,可以正常运行。小心哦

2.如果遇到报:表名无效,且jvm报严重错误,看看是不是用了数据库的保留关键字来做表名了,如user。

3.使用ant时必须小心ant的classpath它用的不是ide的classpath,小心!

4.小心下边的异常,
exception setting property value with cglib (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of xp.bean.users.?" type="net.sf.hibernate.propertyaccessexception">java.lang.classcastexception at xp.bean.usersmetaclass1.setpropertyvalues(<generated>) at net.sf.hibernate.persister.abstractentitypersister.setpropertyvalues
上次出现该错错误的原因是:
源文件里我的class类型为一个类contact contact
而跑到mapping里却成了set,呵呵厉害。

5.突然间冒出大量的nullpointexception
重新build一下看看先。

6.="flush during cascade is dangerous - this might occur if an object was deleted and then re-saved by cascade"

7.tapestry的出错信息:
class com.bookshop.hello does not implement the ipage interface.
location: context:/web-inf/home.page, line 6
原因:与显示有关的哪个java类没有从ipage家族继承。

8.使用ant时给出的路径好象不允许出现空格。

9.由于使用ant时用junit做测试的话,classpath中出现j2ee.jar的话,问题多多。例如xml格式的log文件生成不了,莫名其妙的nullpointexception等。所以我设置了两个包个包含j2ee.jar,为编译用;一个没有,为junit用

10.进行单元测试时,在查询返回后应马上assertnotnull(),这样可以更快速的定位nullpointexception

11.要使用resourcebundle的话,要千万小心。必须用日志记录下它的状态。还有该属性文件应该放在classes下面。

12.当要显示任何页面时,都被提示无效,那么应该检查一下lib目录了,还有一些很奇怪的异常,例如你明明可以找到一个类但是服务器却提示classnotdefexception,那么估计是缺少了该类所必须的包了。或者多了不兼容的包,如, xdoclet系列包不被struts的lib目录所兼容

13.编写clone时从java编程思想(2nd)上学到的(732):
.引数传递过程中会自动产生别名(alias)。
.没有局域对象(local objects),只有局域性的(local)references。
.reference受范围(scope)的限制,对象则否。
.对象的寿命从来不是java的讨论议题(因为有垃圾回收机制)

14.try catch finally的域居然是分离的。

15.jsp乱码的其中一个原因:charset="gb2312" 等号"="的两边不允许有空格。

16.我的基于displaytag的简单报表解决方案。
http://displaytag.sourceforge.net/
下载displaytag.jar和displaytag.tld
displaytag.jar放在lib目录,而displaytag.tld放在web-inf目录,在web.xml中为displaytag.tld声明一下。
  <taglib>
    <taglib-uri>http://displaytag.org</taglib-uri>
    <taglib-location>/web-inf/displaytag.tld</taglib-location>
  </taglib>
在jsp里使用前,加上
  <%@ taglib uri="http://displaytag.org" prefix="display" %>
       
注意该软件有个bug,他要用的一个包common-lang.jar版本必须在2.0以上。
如果碰到下面异常,则应坚持一下是否该包的版本问题。
java.lang.nosuchmethoderror: org.apache.commons.lang.stringutils.capitalize(ljava/lang/string;)ljava/lang/string;
然后就可放心使用了
<display:column property = "xxx"/>其中xxx为对象中的带有getter的变量>。
定义表格的样子,用css定义。如
table.its thead tr {
 background-color: #69c
}
table.its tr.even {
 background-color: #def
}
在使用分页时,可能会出现这种情况,点击其他页时,弹出下载窗口,让你下载当前jsp页面,这是因为你在当前页面读取了数据的缘故。解决办法为在action里读取数据而不是在jsp里。可以参考
http://www.displaytag.org/example-paging.jsp?d-26189-p=2

17.使用displaytag时,在一列中放入多个元素
必须在display:table中定义一个id
<display:table name = "allbooks" class = "its" pagesize = "5" id = "item">
  <display:column title = "操作">
    <a href = "/bookshop/admin/bookmanagement.do?action=view&id=<%=((book)item).getid()%>" target = "_blank" >查看</a>
    <a href = "/bookshop/admin/bookmanagement.do?action=edit&id=<%=((book)item).getid()%>">编辑</a>
    <a href = "/bookshop/admin/bookmanagement.do?action=delete&id=<%=((book)item).getid()%>">删除</a>
  </display:column>
要在displaytag中使用链接,必须具备paramid,否则不显示为链接
 <display:column property = "product.name" href = "viewdetailv2.0.jsp" title = "书名" paramid="item" paramproperty="product.id"/>
可以这样使用display
  <display:column property = "product.id" title = "id"/>
其中product为对象

18.<bean:write name = "xxxx" property = "xxx"/> 可以直接取到session.getattribute()取到的东西。

19.实验struts-upload例子时要注意的地方:
   1.if you would rather write this file to another file, please check here:
 这一行要打钩
   2.if you checked the box to write to a file, please specify the file path here:
在这里要重命名如:c:/b.jpg
上传成功的话,会出现提示 the file has been written to "c:/b.jpg"

20.hibernate的like可以这么用:
      query query = session.createquery("from src.persistent.book as book where upper(book.name) like :name ");
      query.setstring("name", "%");
      result = query.list();

21.hibernate出现 duplicate import : classname
异常也可能是因为忘了为持久类在configuration中addclass了
     
22.<logic:iterate id = "author" name = "authors">
name所引用的是session里的attribute。

23.如果发现要出现询问下载的情况,有可能是因为要跳转的页面出现了问题。试试在要跳转到的页删掉
<%@ page contenttype="text/html; charset=gb2312"%>

24.如果jsp页面跳转时出现下边的错误信息:
the request sent by the client was syntactically incorrect (invalid path /web/shoppingcart was requested).
原因是struts-config的action = "x" 写成了 action = "x.do"

25.在struts中,strut-config.xml中,forward时使用redirect = "true"可以将.do重定向为.jsp

26.以后在判断相等性之前先用logger把两个值显示出来。

27.从session里getattribute后,修改并不需要重新setattribute一次。

28.在hibernate中使用subclass是一棵继承树共用一个表,仅生成个mapping。
每个类中必须有discrimator-value。在最上层的类中必须声明:
@hibernate.discriminator column = "class"。
不可以将子类添加到configuration里去。(即不可addclass(子类))
使用xdoclet的建立subclass的例子(该类是父类)
/**
 * @hibernate.class discriminator-value = "customer"
 * @hibernate.discriminator column = "class"
 */

而使用joined-subclass则是一类一表,也不许将子类添加进configuration里去。
/**
 * @hibernate.joined-subclass
 * @hibernate.joined-subclass-key
 * column="customer_id"
 */
如果发现生成的mapping文件中joined-subclass的key column为空,那么可能是@hibernate.joined-subclass-key这句没有写对。
用joined-subclass生成的表,仔细看。member extends customer
create table customer (
   id varchar2(255) not null,
   name varchar2(255),
   description varchar2(255),
   primary key (id)
)
create table member (
   customer_id varchar2(255) not null,
   password varchar2(255),
   primary key (customer_id)
)

29.在junit中尽量使用assertequals代替asserttrue;

30.hibernate
如果child extends parent
那么from parent as parent 也将会将child选出来,而from child as child 则不会选出parent

31.hibernate
使用hibernate的one-to-one时,应该两方向都set,否则会报save nullpointexception
parent.setchild(child);
child.setparent(parent);

32.hibernate
遇到下边的异常,估计是与因为外键出现了问题:

java.lang.nullpointerexception
 at net.sf.hibernate.persister.abstractentitypersister.getpropertyvalue(abstractentitypersister.java:675)
 at net.sf.hibernate.id.foreigngenerator.generate(foreigngenerator.java:33)
如:
* @hibernate.id generator-class = "foreign"
* @hibernate.generator-param name = "property" value = "customer"
* @hibernate.one-to-one name = "custmoer" class = "src.persistent.customer"
value的值和one-to-one 中name的值不符,则会出现上边的异常。如果不显示指定name则默认取成员变量名

customer a
则name = "a" 需要小心的是不是类名。所以,以后最好显示指定名字为好。

33.出现异常:
exceptionobject references an unsaved transient instance - save the transient instance before flushing: src.persistent.product
原因没有为某对象进行set设置, 如上边的这个就是某对象没有调用setproduct

34.
"xxx action = "/a" 不用.do和根目录名
<a href = "b.do" 需要.do和根目录名

35.
caused by: java.sql.sqlexception: ora-02291: 违反完整约束条件 (bookshop.fk4aaee
47687cca6b) - 未找到父项关键字
如果你觉得该做的e是出现这个问题,那么检查一下,是否将类的继承关系在hibernate的mapping中反映了出来。joined-subclass或subclass
例如a extends b
如果持久类c 需要set的是持久类a,那么你把b传入,而b又没有在mapping中将父子关系反映出来的话,就会出现该异常

36.使用dynaactionform需要注意的问题
在struts-config声明
<form-bean name="memberloginform" dynamic ="true" type="org.apache.struts.action.dynaactionform">
  <form-property name = "name" type = "java.lang.string"/>
  <form-property name = "password" type = "java.lang.string"/>
</form-bean>
在action里将form强制转化成dynaactionform,然后get("属性名")就可以了

37.那位大侠能给出一个用hibernate进行分页得实际例子,学习中!
http://forum.hibernate.org.cn/viewtopic.php?t=1772
query q = s.find(".....");
q.setfirstresult(10);   //  从第11条记录开始
q.setmaxresults(100);  //  取出100条
......

38.struts的validate最简单实现
1.首先准备好错误提示信息。
xxx.properties 里
errors.required={0} is required.(默认已有)
2.form必须从validatorform继承
3.不可以重载validatorform的validate函数
4.在validate.xml中为你想验证的表单进行验证设计。例如
<form name="logonform">
    <field property="username" depends="required">
 <arg0 key="prompt.username"/>
 (该参数将在显示错误信息是从xxx.properties读取prompt.username,填入{}方括号里,取代0。如果是arg1将将填入{1}位置,以此类推。
    </field>
</form>
不需要在action里做任何处理。只管forward就行了。作为forward的目标页,不需要任何有关用于处理出错信息的处理。

39.服务器报
the requested resource (/xxxx/xxx.htm) is not available.的很奇怪的一个的可能原因
在web.xml中定义的tld,没有找到
或者是lib目录下的包太多出现了问题。

  <taglib>
    <taglib-uri>/spring</taglib-uri>
    <taglib-location>/web-inf/spring.tld</taglib-location>
  </taglib>
如web-inf目录下不存在spring.tld的话,就会报上边的错误

40.
spring的xxx-servlet.xml的使用simpleformcontroller系列的类问题:
  <bean id = "priceincreaseform" class="priceincreaseformcontroller">
    <property name="sessionform"><value>true</value></property>
    <property name="beanname"><value>priceincrease</value></property>
    <property name="commandclass"><value>priceincrease</value></property>
    <property name="formview"><value>priceincrease</value></property> 
    <property name="successview"><value>hello</value></property>
    <property name="productmanager">
      <ref bean="prodman"/>
    </property>
  </bean>
这里要注意几个问题:
(1)上边的priceincrease是了类名,必须在classes里存在该类,否则报:
propertyvetoexceptionsexception: 1 errors:-- errorcodedpropertyvetoexception: message=[failed to convert property value of type [java.lang.string] to required type [java.lang.class] for property named 'commandclass'; nested exception is:
 java.lang.illegalargumentexception: invalid class name [priceincrease]: priceincrease]; errorcode=[typemismatch]
java.lang.illegalargumentexception: invalid class name [priceincrease]: priceincrease
(2)<property name="formview"><value>priceincrease</value></property>
这一行必不可少,priceincrease是页面的名字,他将会是prefix + priceincrease + suffix
如果缺少该行,则报:
javax.servlet.servletexception: error in modelandview object or view resolution encountered by servlet with name 'pocketspring': view to render cannot be null with modelandview [modelandview: materialized view is [null]; model=[{priceincrease=priceincrease@148e798, org.springframework.validation.bindexception.priceincrease=org.springframework.validation.bindexception: bindexception: 0 errors}]]

使用errors的rejectvalue相关问题:
rejectvalue(java.lang.string field, java.lang.string errorcode, java.lang.object[] errorargs, java.lang.string defaultmessage)
reject the given field of the current object, using the given error description.
当前对象指的是xxx-servlet.xml中与validator类有联系的哪个对象。
         
第一个是:当前对象的属性名,必须存在
第二个是:将要从属性文件中读取的消息
第三个是:传递给所读取的消息的参数,如:
error.too-low=you have to specify a percentage higher than {0}!
第四个是:当从属性文件中读取消息不成功时,所reject的值

以后从request中读取parameter可以借用spring的requestutils包里的
getstringparameter
public static java.lang.string getstringparameter(javax.servlet.http.httpservletrequest request,
                                           &bsp;      java.lang.string name,
                                                  java.lang.string defaultval)
get a string parameter, with a fallback value. never throws an exception. can pass a distinguished value to default to enable checks of whether it was supplied.
不会抛异常
hibernate的问题:2.1rc的问题
info: cache provider: net.sf.ehcache.hibernate.provider
net.sf.hibernate.hibernateexception: could not instantiate cacheprovider:
解决办法,2.1rc比以前的版本多需要一个包
ehcache.jar

43.static的问题
static函数只可以访问static成员变量。
而static变量可以被任何成员函数访问。

44.
java.lang.noclassdeffounderror: javax/transaction/synchronization];
需要添加jta.jar

扫描关注微信公众号