I am trying to display database records on my JSP page in Struts 2 using Hibernate. I have successfully completed a search.
But no matter what I do, I can not show the data on the JSP page.
I have tried various solutions found on the Internet. But I can’t understand that this is a problem. I can see the table column name, but there is no data in it. I have all the necessary getters and setters in the User POJO class.
I attached my code:
Registration 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();
UserDao :
public class UserDao{ List<User> allUsers = new ArrayList<User>(); public UserDao() {}
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; }
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 ]