当前位置: 代码迷 >> Java Web开发 >> hibernate查询数据并在JSP页面显示(求代码)解决思路
  详细解决方案

hibernate查询数据并在JSP页面显示(求代码)解决思路

热度:2798   发布时间:2013-02-25 21:18:25.0
hibernate查询数据并在JSP页面显示(求代码)
我想实现管理员登录成功以后然后在相应的JSP页面显示用户信息 (用户名 Email 密码 创建时间)
通过hibernate查询数据库 然后通过Struts.xml文件调用相应的display.jsp显示具体的代码如下:
package com.manager.Action;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.manager.hibernate.UserTable;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class DisplayAction extends ActionSupport implements Action {

private String username;
private String userpass;
private Date time;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

@Override
public String execute() throws Exception {

UserTable ut=new UserTable();
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();
Query q=session.createQuery("From UserTable");
往下的代码就不会写了。。。。
}
}

display.jsp页面代码:

<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%>
  <%@ page import="com.manager.Action.DisplayAction" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<center>
<table width="600" border="1">
<tr>
<td>用户名</td><td>密码</td><td>创建时间</td><td>Email</td>
</tr>
<tr>
<td>username</td><td>userpass</td><td>time</td><td>email</td>
</tr>
</table>
</center>
</body>
</html>


上面的JSP页面大体上是这样 但是调用DisplayAction.java里面查询的数据库信息的代码也不知道怎么写
用户的信息都放在一个usertable里面
新手学习,哪位高手有时间帮我把代码写上 不胜感激!

------解决方案--------------------------------------------------------
UserTable ut=new UserTable(); 
SessionFactory sf=new Configuration().configure().buildSessionFactory(); 
Session session=sf.openSession(); 
Query q=session.createQuery("From UserTable"); 

List list=q.list();

再将list中的东西一个一个set到username useremail中应就可以了

至于前台面页显示用${requestScope.username} ${requestScope.useremail} 就能显示出来
------解决方案--------------------------------------------------------
public String execute() throws Exception { 

UserTable ut=new UserTable(); 
SessionFactory sf=new Configuration().configure().buildSessionFactory(); 
Session session=sf.openSession(); 
Query q=session.createQuery("From UserTable"); 
List list=q.list();
request.setAttribute("blist",list);




在你的页面上用:
<tr> 
<td>用户名 </td> <td>密码 </td> <td>创建时间 </td> <td>Email </td> 
</tr> 
<tr> 

<logic:notEmpty name="blist">
  相关解决方案