本文介绍了如何在Struts 2中显示数据库记录(通过Hibernate检索)到JSP页面的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图在Struts 2中使用Hibernate将数据库记录显示到我的JSP页面。
我已经成功完成了检索。



但是无论我做什么,我都无法在JSP页面中显示数据。 p>

我尝试过在互联网上找到的各种解决方案。但不知道什么似乎是问题。
我可以看到表列的名称,但其中没有数据。
我在我的用户POJO类中拥有所有必需的getter和setter。



我附上了我的代码:

注册操作:

   session 的code>属性是一个灾难。 DAO不应该有 static 属性。如果你想获得一个会话,使用 HibernateUtil.getSession(),并且不要忘记在事务结束时关闭会话。我想你还是没有实现我在,所以你不知道如何从线程获取会话。无论如何,在构造函数中打开一个会话只在第一次使用会话时有效,并且在关闭会话后不再可用。 

接下来, ModelDriven 最好由@Quaternion描述,关于您的模型的几句话:您的模型仅用于查看用户,并且不包含显示用户。
$ b

最后,方法 execute 是动作配置使用的默认方法,您不应该映射此方法,除非你知道你在做什么。

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]
解决方案

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.

这篇关于如何在Struts 2中显示数据库记录(通过Hibernate检索)到JSP页面的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:52