【问题标题】:Populate Dropdownlist with Database value in Struts 2, Hibernate在 Struts 2、Hibernate 中使用数据库值填充下拉列表
【发布时间】:2013-09-26 18:46:43
【问题描述】:

我正在尝试使用表格中的值填充下拉列表。该页面使用 strust2 和 hibernate 访问数据库。

我可以查看注册页面(其中包含必须填充数据库值的下拉菜单) 但是下拉列表中没有出现任何值。

index.jsp

  <s:url id="url" action="registerAction">

  </s:url>
  <s:a href="%{url}">Register</s:a>

register.jsp

 <s:form action="registerAction">
    <s:select label="Select Date of Month" name="months" headerKey="0" headerValue="--Select--"
                      list="allMonths" listKey="id" listValue="name"/>
    <s:submit value="Register"/>

 </s:form>

struts.xml

<action name="registerAction" class="action.RegisterAction" method="populateSelect">
       <result name="none">register.jsp</result>
</action>

月份模型

@Entity
@Table(name="tbl_mnth")
public class Month {
    @Id
    @GeneratedValue
    @Column(name="mnth_id")
    private int id;
    @Column(name="mnth_name")
    private String name;

    public Month(){}

    public Month(String name) {
        this.name=name;
    }

    public String getName() {
        return name;
    }
}

DAO

public class UserDao {

 List<Month> allMonths = new ArrayList<Month>();
public List<Month> getAllMonths() {
        return allMonths;
    }

    public void setAllMonths(List<Month> allMonths) {
        this.allMonths = allMonths;
    }

    public Session getSession() {
        return HibernateUtil.getSession();
    }

    public void closeSession() {
        HibernateUtil.closeSession();
    }

    public UserDao() {
    }

    public List<Month> getMonths() {
        Session session = getSession();
        Transaction t = session.beginTransaction();
        allMonths = (List<Month>) session.createQuery("FROM Month").list();
        System.out.println("In constructor " + allMonths);
        t.commit();
        closeSession();
        return allMonths;
    }
}

动作类

public class RegisterAction extends ActionSupport {
List<Month> allMonths = new ArrayList<Month>();
String months;
UserDao udao = new UserDao();

public List<Month> getAllMonths() {
        return allMonths;
    }

    public void setAllMonths(List<Month> allMonths) {
        this.allMonths = allMonths;
    }
    public String getMonths() {
        return months;
    }

    public void setMonths(String months) {
        this.months = months;
    }
    public String populateSelect() {
        allMonths = udao.getMonths();
        System.out.println("In constructor " + allMonths);
        return "none";
    }
    public RegisterAction() {}
}

数据库表为tbl_mnth,字段为mnth_idmnth_name

我从中猜想的是动作类没有被实例化。我不知道它如何或为什么没有被实例化..

在服务器日志中显示

org.hibernate.hql.ast.QuerySyntaxException: Month is not mapped [from Month]

【问题讨论】:

    标签: java hibernate jsp orm struts2


    【解决方案1】:

    尝试在您的 DAO 中执行此操作

    public List<Month> getMonths() {
        Session session = getSession();
        Transaction t = session.beginTransaction();
        allMonths = (List<Month>) session.createCriteria(Month.class).list();
        System.out.println("In constructor " + allMonths);
        t.commit();
        closeSession();
        return allMonths;
    }
    

    顺便说一句,这里与您的操作类无关,因为异常显然是休眠异常。尝试在您的操作类中放置一个断点(如果您使用的是 IDE),否则放置一条 System.out.println 语句以查看您的代码是否正在进入操作类。

    【讨论】:

    • 错误是由于没有在休眠配置文件中添加映射..由 Roman C 指出..谢谢你也伸出你的手..
    【解决方案2】:

    Hibernate 是一个 ORM(对象-关系-映射)框架,用于将 POJO 映射到数据库模式。在您的情况下,它是Month。并且因为您使用注释将对象映射到表,所以您应该在 hibernate.cfg.xml 中配置它

    <mapping class="package.to.persistence.Month" />
    

    如果没有映射类 Hibernate 无法在您的对象中找到注释。

    【讨论】:

    • 它有效..谢谢你..我的错误..在互联网上花费至少 2 周搜索这个问题..
    猜你喜欢
    • 2013-09-30
    • 2012-09-26
    • 2014-08-27
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多