How to map a list of database records (retrieved via Hibernate) to a JSP page in Struts 2?

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(); //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 ] 
+6
source share
3 answers

Maybe you understand, but don’t know why you didn’t try to resolve it. If you want to display data, you must first put it in the database. Verify data availability by connecting it to the client application. There are many ways to connect to the database, including the JDBC client application that comes with the IDE. It also uses connection properties directly from your hibernate.cfg.xml and has the ability to test the connection. Also, make sure that the credentials used to connect to the database have DML / DDL authority to the schema, which should probably be created manually.

This file is intended for the configuration of sleep mode, so you must pay attention to it in order to be valid due to the correct DTD corresponding to the version of sleep mode.

Then you use annotation-based mapping, and it must also be configured in the configuration file.

Further, the DAO should not extend HibernateUtil , and the static property for session is a disaster. DAOs must not have static properties. If you want to use the HibernateUtil.getSession() session and remember to close the session at the end of the transaction. I think you still haven’t implemented what I suggested in the previous answer , so you don’t know how to get the session from the stream. In any case, opening a session in the constructor only works the first time you use a session, and it is no longer available after it is closed. Before starting a transaction, open a session in a method.

Next, ModelDriven better described by @Quaternion, a few words about your model: your model is used only for viewing user and does not contain properties for displaying users .

Finally, the execute method is the default method used by setting up the action; you should not map this method if you do not know what you are doing.

+4
source

I found that your jsp list property name and class action class name must match. In my understanding, this should solve your problem .....!

0
source

Override toString() method in User.java

0
source

Source: https://habr.com/ru/post/953405/


All Articles