I am trying to display the database records to my JSP page in Struts 2 using Hibernate. I have done the retrieval part successfully.
But no matter what I do, I can't seem to display the data in the JSP page.
I have tried various solution found in internet. But cant understand whats seems to be the problem. I can see the tables column name but there is no data in it. I have all the required getters and setters in my User POJO class.
I have attached my code:
Register action:
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 Class:
@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();
}
}
The server log contains
[models.User@9c0fad, models.User@1c94f2c, models.User@16d06ef]
INFO: In Action, [models.User@9c0fad, models.User@1c94f2c, models.User@16d06ef]