在execute()方法的return前面将所有与DAO有关的变量设成null,就不会出错了。
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="default" extends="json-default">
<action name="login" class="com.whm.controller.LoginAction">
<result type="json"></result>
</action>
<action name="showGoodsName" class="com.whm.controller.ShowGoodsNameAction">
<result type="json"></result>
</action>
</package>
</struts>
ShowGoodsNameAction.java
package com.whm.controller;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.whm.service.impl.GoodsNameService;
import com.whm.service.impl.PageBean;
public class ShowGoodsNameAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 8029696367199852252L;
private GoodsNameService goodsNameService;
/**第几页*/
private int page;
/**包含分布信息的bean*/
private PageBean pageBean;
/**hql查询语句*/
private String hql;
public void setGoodsNameService(GoodsNameService goodsNameService)
{
this.goodsNameService = goodsNameService;
}
public GoodsNameService getGoodsNameService()
{
return this.goodsNameService;
}
public int getPage()
{
return page;
}
public void setPage(int page)
{
//若URL中无此参数,会默认为第1页
this.page = page;
}
public PageBean getPageBean()
{
return pageBean;
}
public void setPageBean(PageBean pageBean)
{
this.pageBean = pageBean;
}
public String getHql()
{
return this.hql;
}
public void setHql(String hql)
{
this.hql=hql;
}
/**清理无用的变量,特别是与Hibernate关联的类,否则转化为json时会出错*/
private void clearNotUsedVar()
{
goodsNameService=null;
}
@Override
public String execute() throws Exception
{
HttpServletRequest request = ServletActionContext.getRequest();
int start = Integer.valueOf(request.getParameter("start"));
int limit = Integer.valueOf(request.getParameter("limit"));
String sort= request.getParameter("sort");//要排序的列名--无需定义,ext自动后传
String dir= request.getParameter("dir");//要排序的方式--无需定义,ext自动后传
this.page=start;
if(sort==null) sort="name";
if(dir==null) dir="asc";
this.hql="from WhmGoodsName order by "+sort+" "+dir;
System.out.println("访问ShowGoodsNameAction");
System.out.println("page:"+page);
System.out.println("limit:"+limit);
System.out.println("sort:"+sort);
System.out.println("dir:"+dir);
//分页的pageBean,参数pageSize表示每页显示记录数,page为当前页
this.pageBean = goodsNameService.queryForPage(this.hql,limit, page);
//清除,全部设成null
this.clearNotUsedVar();
return SUCCESS;
}
}