我正在尝试使用Hibernate和PrimeFaces创建一个带有MySQL数据库的Java和JSF项目。
Hibernate层运行良好(基本CRUD工作正常),但在使用Prime Faces进行基本GUI并将项目部署到Tomcat,server之后,它返回错误:
Unable to create managed bean DBUserMBean. The following problems were
found: - Bean or property class
com/hibernate/maven/Hibernate_APP/DBUserManagedBean for managed bean
DBUserMBean cannot be found.
以下是我项目中的所有文件:
DBUSER
@Entity
public final class DBUser{
private Long id;
private String kolumna1;
private String kolumna2;
private String kolumna3;
private int kolumna4;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String toString() {
StringBuffer strBuff = new StringBuffer();
strBuff.append("id :").append(id);
strBuff.append("kolumna1 :").append(kolumna1);
strBuff.append("kolumna2 :").append(kolumna2);
strBuff.append("kolumna3 :").append(kolumna3);
strBuff.append("kolumna4 :").append(kolumna4);
return strBuff.toString();
}
}
DBUserManagedBean
@ManagedBean(name="DBUserManagedBean")
@SessionScoped
public class DBUserManagedBean implements Serializable{
private static final long serialVersionUID = 1L;
private static Logger log = Logger.getLogger(DBUserManagedBean.class);
private static final String SUCCESS = "success";
private static final String ERROR = "error";
private Long id;
private String kolumna1;
private String kolumna2;
private String kolumna3;
private int kolumna4;
private String message;
//getters and setters
public String getMessage() {
StringBuffer strBuff = new StringBuffer();
strBuff.append("id :").append(id);
strBuff.append("kolumna1 :").append(kolumna1);
strBuff.append("kolumna2 :").append(kolumna2);
strBuff.append("kolumna3 :").append(kolumna3);
strBuff.append("kolumna4 :").append(kolumna4);
this.setMessage(strBuff.toString());
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public String save() {
String result = null;
Session session = HibernateUtil.getSessionFactory().openSession();
DBUser dbuser = new DBUser();
dbuser.setKolumna1(this.getKolumna1());
dbuser.setKolumna2(this.getKolumna2());
dbuser.setKolumna3(this.getKolumna3());
dbuser.setKolumna4(this.getKolumna4());
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(dbuser);
tx.commit();
log.debug("New Record : " + dbuser + ", wasCommitted : " + tx.wasCommitted());
result = SUCCESS;
} catch (Exception e) {
if (tx != null) {
tx.rollback();
result = ERROR;
e.printStackTrace();
}
} finally {
session.close();
}
return result;
}
public List<DBUser> getDBUsers() {
Session session = HibernateUtil.getSessionFactory().openSession();
List<DBUser> dbuserList = session.createCriteria(DBUser.class).list();
return dbuserList;
}
public void reset() {
this.setKolumna1("");
this.setKolumna2("");
this.setKolumna3("");
this.setKolumna4(kolumna4);
}
}
面-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>DBUserMBean</managed-bean-name>
<managed-bean-class>com.hibernate.maven.Hibernate_APP.DBUserManagedBean</managed-
bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>pages/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>pages/welcome.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>error</from-outcome>
<to-view-id>pages/error.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
的index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head><title>Hibernate_APP</title></h:head>
<body>
<h:form>
<table>
<tr>
<td><h:outputLabel for="kolumna1" value="kolumna1 :" /></td>
<td><p:inputText id="kolumna1" value="#{DBUserManagedBean.kolumna1}"/></td>
</tr>
<tr>
<td><h:outputLabel for="kolumna2" value="kolumna2 :" /></td>
<td><p:inputText id="kolumna2" value="#{DBUserManagedBean.kolumna2}"/></td>
</tr>
<tr>
<td><h:outputLabel for="kolumna3" value="kolumna3 :" /></td>
<td><p:inputText id="kolumna3" value="#{DBUserManagedBean.kolumna3}"/></td>
</tr>
<tr>
<td><h:outputLabel for="kolumna4" value="kolumna4 :" /></td>
<td><p:inputText id="kolumna4" value="#{DBUserManagedBean.kolumna4}"/></td>
</tr>
<tr>
<td><p:commandButton id="submit" value="Save" action="#
{DBUserManagedBean.save}" ajax="false"/></td>
<td><p:commandButton id="reset" value="Reset" action="#
{DBUserManagedBean.reset}" ajax="false"/></td>
</tr>
</table>
</h:form>
</body>
</html>
welcome.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<title>Hibernate_APP</title>
</h:head>
<body>
<h:form>
<h:outputText value="Saved Record is #{DBUserMBean.message}"></h:outputText>
<p:dataTable id="DBUsers" value="#{DBUserMBean.getDBUsers()}" var="DBUser"
style="width: 10%">
<p:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet>
<h:outputText value="#{DBUser.id}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="kolumna1" />
</f:facet>
<h:outputText value="#{DBUser.kolumna1}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="kolumna2" />
</f:facet>
<h:outputText value="#{DBUser.kolumna2}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="kolumna3" />
</f:facet>
<h:outputText value="#{DBUser.kolumna3}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="kolumna4" />
</f:facet>
<h:outputText value="#{DBUser.kolumna4}" />
</p:column>
</p:dataTable>
</h:form>
</body>
</html>
当我尝试通过表单将数据保存到数据库时,这是个例外:
WARNING: /pages/index.xhtml @14,87 value="#{DBUserManagedBean.kolumna1}":
Target Unreachable, identifier 'DBUserManagedBean' resolved to null
javax.el.PropertyNotFoundException: /pages/index.xhtml @14,87
value="#{DBUserManagedBean.kolumna1}": Target Unreachable,
identifier 'DBUserManagedBean' resolved to null
答案 0 :(得分:0)
您缺少@Table(name="your Table Name")
和@column
注释。
删除
的方法签名value="#{DBUserMBean.getDBUsers()}"
试试这个
value="#{DBUserMBean.getDBUsers}"
答案 1 :(得分:-1)
至于我的帖子没有回答这个问题,我只会将评论留给作者的代码。
您的托管bean在faces-config.xml
和@ManagedBean
注释中都会声明两次。虽然它看起来可能会产生歧义,但faces-config.xml
优先于带注释的配置。尽管如此,根据我的口味,两次命名bean并使用不同的名称,可能会导致多个错误。所以我建议用注释或xml格式命名bean。
请注意,faces-config.xml
是定义托管bean和导航案例结果的老派方式。 JSF 2.x支持注释和隐式导航。
顺便说一下,你的@Entity
类缺少@Column
和@Table
的注释,你的支持bean应该有你的实体的DTO,分离的实体本身,插入多个文件你做的是一个不好的做法,你的行动方法应该返回导航案例结果,如welcome
等。
最后,按照@BalusC在评论#3中正确地说出来,并检查xml配置文件中的完全限定名称和/或确保您的类文件实际存在于您的构建中。