首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

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

2012-06-10 
hibernate查询数据并在JSP页面显示(求代码)我想实现管理员登录成功以后然后在相应的JSP页面显示用户信息 (

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">


<logic:iterate id="dlist" name="blist" >
<bean:write name="dlist" property="useName"/>
<bean:write name="dlist" property="password/>
....bean的其他属性.
</logic:iterate>
</logic:notEmpty>

[解决办法]
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; 
/** 添加 **/
private Usertable user;
public Usertable getUser(){
return this.user;
}
/**到这里**/
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"); 
往下的代码就不会写了。。。。 
/**添加**/
List list=q.list(); 
if(list.size() > 0) {
 user = (Usertable)list.get(0);
}
else{
user = null;
}
/**到这里**/



显示,首先要加上STRUTS的标签,这个例子到处都有,就不举例了

然后
<table width="600" border="1"> 
<tr> 
<td>用户名 </td> <td>密码 </td> <td>创建时间 </td> <td>Email </td> 
</tr> 
<tr> 
<s:if test = "user!=null">
<td><s:property value="user.username"/> </td> <td><s:property value="user.userpass"/></td> <td><s:property value="user.time"/></td> 
</s:if>
<s:else>
没有该用户的信息
</s:else>
</tr> 
</

热点排行