我正在尝试使用 Hibernate 在 Struts 2 中的 JSP 页面中显示数据库记录。
我已经成功完成了检索部分。
但是无论我做什么,我似乎都无法在 JSP 页面中显示数据。
我尝试了在互联网上找到的各种解决方案。但无法理解似乎是什么问题。
我可以看到表格列名,但其中没有数据。
我的User
POJO 类中有所有必需的 getter 和 setter。
我附上了我的代码:
注册动作:
public class RegisterAction extends ActionSupport{
String name,pwd,email,address;
int phno;
public RegisterAction() {}
List<User> users = new ArrayList<User>();
UserDao udao = new UserDao();
//Getters and setters.
public String execute() throws Exception {
User u=new User();
u.setName(name);
u.setEmail(email);
u.setAddress(address);
u.setPhno(phno);
u.setPwd(pwd);
udao.addUser(u);
return "success";
}
public String listAllUsers(){
users = udao.getUsers();
System.out.println("In Action, "+users);
return "success";
}
}
UserDao
:
public class UserDao{
List<User> allUsers = new ArrayList<User>();
public UserDao() {}
//Getter and setter.
public Session getSession(){
return HibernateUtil.getSession();
}
public void closeSession(){
HibernateUtil.closeSession();
}
public void addUser(User u) {
Session session= getSession();
Transaction t = session.beginTransaction();
int i = (Integer)session.save(u);
t.commit();
closeSession();
}
public List<User> getUsers() {
Session session=getSession();
Transaction t = session.beginTransaction();
allUsers = (List<User>)session.createQuery("from User").list();
t.commit();
closeSession();
System.out.print(allUsers);
return allUsers;
}
}
User.java
//实体类:
@Entity
@Table(name="tbl_user")
public class User {
@Id
@GeneratedValue
@Column(name="user_id")
private int id;
@Column(name="user_phno")
int phno;
@Column(name="user_name")
private String name;
@Column(name="user_pwd")
private String pwd;
@Column(name="user_email")
private String email;
@Column(name="user_address")
private String address;
public User(){}
public User(String name,String pwd,String email,String address,int phno){
this.name = name;
this.pwd = pwd;
this.email = email;
this.address =address;
this.phno = phno;
}
//Getters and setters.
}
home.jsp
:
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>Phone No</th>
</tr>
<s:iterator value="users">
<tr>
<td><s:property value="name"/></td>
<td><s:property value="email"/></td>
<td><s:property value="address"/></td>
<td><s:property value="phno"/></td>
</tr>
</s:iterator>
</table>
struts.xml
:
<action name="register" class="action.RegisterAction" method="execute">
<result name="success" type="redirect">listUsers</result>
</action>
<action name="listUsers" class="action.RegisterAction" method="listAllUsers">
<result name="success">/home.jsp</result>
</action>
HibernateUtil
:
public class HibernateUtil {
static SessionFactory sessionFactory;
static Session session;
public static Session getSession() {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
session= sessionFactory.openSession();
return session;
}
public void setSession(Session session) {
this.session = session;
}
public static void closeSession(){
session.close();
}
}
服务器日志包含:
[models.User@9c0fad, models.User@1c94f2c, models.User@16d06ef]
INFO: In Action, [models.User@9c0fad, models.User@1c94f2c, models.User@16d06ef]