我正在尝试使用表中的值填充下拉列表。该页面使用 strust2 和 hibernate 访问数据库。
我可以查看注册页面(其中包含必须填充数据库值的下拉列表)但下拉列表中没有出现任何值。
索引.jsp
<s:url id="url" action="registerAction">
</s:url>
<s:a href="%{url}">Register</s:a>
注册.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;
}
}
道
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_id
, mnth_name
。
我从中猜测的是动作类没有被实例化。我不知道它如何或为什么没有被实例化..
在服务器日志中显示
org.hibernate.hql.ast.QuerySyntaxException: Month is not mapped [from Month]