标签:
实际上是结合struts2来从数据获取json格式的数据。
关键代码:
GetUserAction.java代码
package com.log.control;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import com.log.entity.User4;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用于从数据库获取数据
* @author Wei
* @time 2016年9月19日 上午12:45:39
*/
public class GetUserAction extends ActionSupport {
private User4 user;
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 获取json格式字符串
* @param obj
* @return
*/
public String getJsonString(Object obj) {
ObjectMapper om = new ObjectMapper();
StringWriter sw = new StringWriter();
try {
JsonGenerator jg = new JsonFactory().createJsonGenerator(sw);
om.writeValue(jg, obj);
jg.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sw.toString();
}
/**
* 把从数据库获取到的数据 List<User4> list 写到页面上
* @param obj
*/
public void writeJson(Object obj) {
String json = getJsonString(obj);
try {
ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
ServletActionContext.getResponse().getWriter().write(json);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
*/
@Override
public String execute() throws Exception {
// easyui-datagrid表格 点击下一页的时候 的参数 page=2&rows=11&sort=email&order=DESC
HttpServletRequest req = ServletActionContext.getRequest();
//System.out.println("---11111---page:"+req.getParameter("page"));
//System.out.println("GetUserAction.java----------"+req.getQueryString());
UserDao dao = new UserDao();
//开启hibernate的transaction等
dao.init();
List<User4> list = new ArrayList<User4>();
for(int i=1;i<20;i++){
//通过hibernate从数据库获取数据,放到list中
list.add((User4) dao.session.get(User4.class, i));
}
//销毁hibernate相关的资源
dao.destroy();
//写到页面上
writeJson(list);
return null;
}
}
UserDao.java代码
package com.log.control;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class UserDao {
// private SessionFactory sessionFactory;
// private Session session;
// private Transaction transaction;
public SessionFactory sessionFactory;
public Session session;
public Transaction transaction;
// @Before
public void init() {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
// @After
public void destroy() {
transaction.commit();
session.close();
sessionFactory.close();
}
// @Test
public void testInsert() {
UserDao dao = new UserDao();
dao.init();
for (int i = 43; i < 64; i++) {
dao.session
.save(new User4(55, "xiaohong" + i, "qq" + i + "@163.com", new Date(System.currentTimeMillis())));
}
dao.destroy();
}
}
具体页面展示:
获取到的json数据:
easyui-datagrid通过action从数据库获取数据的关键代码
标签:
原文地址:http://www.cnblogs.com/Sunnor/p/5883683.html