您的位置:首页 > 产品设计 > UI/UE

关于从后台获取数据List<User>转化为JSON格式在前台用easyui以表格显示

2014-10-30 16:41 1071 查看
本来只是想实现用户点击“个人信息”就可以查看自己的信息,而且想用easyui +ajax来表现出来,因为之前在长沙是这么做的,这样实现也挺不错,省去了我美工的时间,现在我 只想把功能弄出来。结果发现需要的只是一个人的信息,不需要用datagrid表现出啦,不过主要中心目的目前还是想达到数据库到前台界面这样的一个功能。

接着上次的SSH项目:

首先把bean中的User添加全面一点:

package com.ssh.bean;

/**
* User entity. @author MyEclipse Persistence Tools
*/

public class User implements java.io.Serializable {

// Fields

private Integer id;
private String username;
private String password;
private String email;
private String address;
private Integer sex;			//0表示女,1表示男

// Constructors

/** default constructor */
public User() {
}

/** full constructor */
public User(String username, String password, String email, String address,
Integer sex) {
this.username = username;
this.password = password;
this.email = email;
this.address = address;
this.sex = sex;
}

// Property accessors

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}

public String getAddress() {
return this.address;
}

public void setAddress(String address) {
this.address = address;
}

public Integer getSex() {
return this.sex;
}

public void setSex(Integer sex) {
this.sex = sex;
}

}


在com.ssh.daoimpl中添加LIst<User> findByUserName(String userName) (这里我就不写Dao,Service,ServiceImpl,这些都是要的,和之前的一样写)

@SuppressWarnings("unchecked")
@Override
public List<User> findByUserName(String userName) {
// TODO Auto-generated method stub
System.out.println(userName+"========UserDaoImpl");
final String s1 = userName;
List<User> list = new ArrayList<User>();
list = (ArrayList<User>) this.getHibernateTemplate().executeFind(
new HibernateCallback() {

@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
// TODO Auto-generated method stub
//System.out.println(session.createQuery("from User u where u.username = '"+s1+"'").toString());
return session.createQuery("from User u where u.username = '"+s1+"'").list();
}

});
System.out.println(list);
return list;
}
在ShowMyinfoAction。java:

package com.ssh.action;
import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.bean.User;
import com.ssh.service.UserService;

@SuppressWarnings("serial")
public class ShowMyInfoAction extends ActionSupport{
private UserService userService;
private String userName;
private String message;

public String execute() throws Exception{

//System.out.println("====here is execute======");
System.out.println(userName);

//JSONArray json = JSONArray.fromObject(list);
//JSONObject json = JSONObject.fromObject(list);
//System.out.println("========"+json+"=========");
try{
ServletActionContext.getResponse().getWriter().print(JSONUtil.serialize(userService.findByUserName(userName)));		//向前台输出json数据
//System.out.println("======finish=========");
}catch(IOException e){
//System.out.println("======here is ShowMyInfoAction=========1");
message = e.getMessage();
}catch(JSONException e){
//System.out.println("======here is ShowMyInfoAction=========2");
message = e.getMessage();
}catch(Exception e){
//System.out.println("======here is ShowMyInfoAction=========3");
message = e.getMessage();
}
return null;

}

public UserService getUserService() {
return userService;
}

public void setUserService(UserService userService) {
this.userService = userService;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}


因为我还没有找到如何获取两个页面前的username,所以就现在代码中直接传入admin而不是动态获取,有待完善:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<div class="body">
<input  id="userName"
value="admin"/><span style="white-space:pre">			</span><!-- 直接传入admin-->

<div title="用户信息" style="padding:10px">
<table id="all"></table>
</div>

<script type="text/javascript">
$(function() {
$('#all').datagrid({
brder : false,
url : 'showMyInfoAction.action',
pagination : true,
pageList : [ 5, 10 , 20],<span style="white-space:pre">		</span>
queryParams : {
userName : $("#userName").val()
},
columns : [[
{
field : 'id',
title : 'id',
width : 100,
align : 'center'
},
{
field : 'username',
title : 'username',
width : 100,
align : 'center'
},
{
field : 'password',
title : 'password',
width : 100,
align : 'center'
},
{
field : 'email',
title : 'email',
width : 100,
align : 'center'
},
{
field : 'address',
title : 'address',
width : 100,
align : 'center'
},
{
field : 'sex',
title : 'sex',
width : 100,
align : 'center'
}

]]

});

})
</script>

</div>


另外不要忘记Sruts2.x和applicationContext.xml中添加的信息。

结果如图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐