最近在做一个进销存的管理系统,是采用的S2SH框架,这是自己第一次真正的参与编码环节的工作,出现了很多的问题,现在把它们记录下来,时刻提醒自己。
1. 错误提示
Caused by: org.hibernate.hql.ast.QuerySyntaxException: spending is not mapped [select count(*) from spending order by id desc.
解決的方法:沒有用到类 而是用的是表,框架为S2SH,检查发现Hibernate配置文件中没有加入spending的映射。
2. 页面上,先执行删除操作,再执行新增操作,错误提示:
attempt to create delete event with null entity
原因:刷新之后,重复提交了删除的请求
解决方法:显示列表时try …catch…,即使抛出异常,也返回至列表显示页面,代码如下:
try{
int id = Integer.parseInt(request.getParameter("id"));
spResnMgr.delete(id);
return this.execute();
}catch(Exception e){
return this.execute();
}
说明:S2SH中,我们这个项目的增是采用弹层的方式,如果是经struts2直接跳转的话,会出现重复增加两次的情况。更新操作不会出现重复提交的情况(因为更新的是同一条记录)。
但是,这种解决方法并不是万能的,有时候,跳转的页面是需要靠其他页面传值过来的。这是,要将所传的值写进catch的语句中,并在刷新返回页面的那个action中判断读出
删除的超链接上将id和pid都传至action中
/**
* 删除数据字典内容信息
* @return
* @throws Exception
*/
public String delete(){
int id = Integer.parseInt(request.getParameter("id"));
int pid = Integer.parseInt(request.getParameter("pid"));
//用于传到toContent()方法的,判断是否由删除方法跳转过来的
request.setAttribute("test", "test");
try {
dataMgr.delete(id);
return this.toContent();
} catch (Exception e) {
request.setAttribute("pid", pid);
return this.toContent();
}
}
/**
* 跳转到显示数据字典内容的列表
* @return
*/
public String toContent(){
String test = (String)request.getAttribute("test");
int pid;
if(test == null){
//正常跳转
pid = Integer.parseInt(request.getParameter("pid"));
}else{
//由删除页面跳转而来
pid = (Integer)request.getAttribute("pid");
}
PageModel pm = dataMgr.findDataContent(15, "where datId = " + pid + " order by id desc");
request.setAttribute("pm", pm);
return "content_list";
}
小注释:request.getAttribute("pid")是Integer类型
3. 关于超链接传值,无法从客户端传到服务器上
问题重现:
超链接为
href="datadictionary!delete.action?pid=<s:property value="id" />"
鼠标移上去的时候,下面地址栏已经显示pid=2,但在后台的action 中,读取pid的值为空,报空指针异常
解决方法:
在action中增加一个pid属性和get set 方法,用于接收传过来的pid的值。
问题原因:不祥
4. DWR学习时报错java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
解决方法:引入commons-logging.jar包