我正在尝试使用 Hibernate 和 PrimeFaces 使用 MySQL 数据库制作 Java 和 JSF 项目。
Hibernate 层运行良好(基本 CRUD 运行良好),但在我使用 Prime Faces 制作基本 GUI 并将项目部署到 Tomcat、服务器后,它返回错误:
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.
这是我项目中的所有文件:
数据库用户
@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>
索引.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>
欢迎.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