SSH中的Hibernate的DAO类转json时出错

 

在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;
		
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值