I am trying to display the database records in 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 solutions found in internet. But can't understand what 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]
May be you understand, but don't know why you didn't any attempt to resolve it. If you want to display data, first you should put it in the database. Check the data is available, connecting to it via the client application. There's many ways to connect to the database, including a JDBC client application supplied with the IDE. It also takes a connection properties right from your hibernate.cfg.xml
and has capability to test the connection. Also, make sure the credential used to connect to the database has DML/DDL access privileges to the schema which should be probably created manually.
This file is for hibernate configuration, that you should take attention on it, to be valid due to a correct DTD corresponding to the version of hibernate.
Then, you are using annotation based mapping, and it also should be configured in the configuration file.
Next, DAO is not supposed to extend the HibernateUtil
and putting a static
property for the session
is a disaster. DAOs should not have static
properties. If you want to get a session use HibernateUtil.getSession()
and don't forget to close session at the end of transaction. I guess you still didn't implement what I have proposed in the previous answer, so you don't know how to get the session from thread. Anyway opening a session in the constructor is only works a first time you use the session, and it's not longer available after you close it. Open a session in the method before you start a transaction.
Next, ModelDriven
is better described by @Quaternion, a few words about your model: your model is only used to view a user
and doesn't contain properties to display users
.
Lastly, method execute
is a default method used by the action configuration, you shouldn't map this method, unless you know what are you doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With