我正在使用JSF和Primefaces从事Java EE项目。我还使用Hibernate作为ORM框架。正如我所看到的,Primefaces和Hibernate并没有干涉,但我提到它以便你可以全面了解。 我有登录和注册页面。两者都是Facelets模板客户端,您可以从第一个导航到第二个。注册页面链接到2个Managed Bean的属性,并从第一个调用方法。 MCVE将是:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<body>
<ui:composition template="./templates/mainTemplate.xhtml">
<ui:define name="top"></ui:define>
<ui:define name="content">
<h:form>
<h2>Login</h2>
<h:panelGrid columns="2">
<h:outputLabel for="user" value="User:" />
<p:inputText id="user"/>
<h:outputLabel for="password" value="Password:" />
<p:password id="password"/>
<!-- Still no connection to the Login's MB -->
<p:commandButton value="Login"/>
<!-- Go to register.xhtml -->
<p:commandButton value="Register" action="register"/>
</h:panelGrid>
</h:form>
</ui:define>
<ui:define name="bottom">bottom</ui:define>
</ui:composition>
</body>
</html>
<!--ui:composition nested in html and body as in index.xhtml-->
<ui:composition template="./templates/mainTemplate.xhtml">
<ui:define name="top"></ui:define>
<ui:define name="content">
<h:form id="frmRegister">
<h:panelGrid columns="2">
<h:outputLabel for="name" value="Name:" />
<p:inputText id="name" label="Name" required="true" size="50"
value="#{personBean.person.name}"/>
<h:outputLabel for="birthdate" value="Date of birth:" />
<p:calendar id="birthdate"
label="Date of birth"
placeholder="dd/mm/aaaa"
required="true"
value="#{personBean.person.birthdate}"/>
<h:outputLabel for="password" value="Password(*):"/>
<p:password id="password" label="Password" required="true" size="30"
value="#{userBean.user.password}"/>
<h:outputLabel for="password-repeat" value="Repeat your password(*):"/>
<p:password id="password-repeat" label="Repeated password" required="true" size="30"
value="#{userBean.repeatPassword}"/>
<p:commandButton value="Register User" actionListener="#{personBean.addPerson()}"/>
<p:commandButton value="Back" action="index" />
</h:panelGrid>
</h:form>
</ui:define>
<ui:define name="bottom">bottom</ui:define>
</ui:composition>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<h:outputStylesheet name="./css/default.css"/>
<h:outputStylesheet name="./css/cssLayout.css"/>
<h:outputStylesheet name="./css/libs/bootstrap.min.css"/>
<h:outputStylesheet name="./css/libs/bootstrap-theme.min.css"/>
<title>Title</title>
</h:head>
<h:body>
<div id="top">
<ui:insert name="top">Top</ui:insert>
<h1>A Test</h1>
</div>
<div id="content" class="center_content">
<ui:insert name="content">Content</ui:insert>
</div>
<div id="bottom">
<ui:insert name="bottom">Bottom</ui:insert>
</div>
问题在于,当我尝试加载页面时,我得到一个空白页面,当我打开Chrome的开发人员工具时,我可以看到正文使用与头部相同的信息进行渲染。得到图片:
<head>
<link type="text/css" rel="stylesheet" href=".../theme.css?ln=primefaces-bootstrap">
...
<script type="text/javascript" src="...jquery.js?ln=primefaces&v=5.0"></script>
...
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>THE TITLE</title>
<link rel="icon" href="resources/img/favicon.ico">
</head>
<body>
<link type="text/css" rel="stylesheet" href=".../theme.css?ln=primefaces-bootstrap">
...
<script type="text/javascript" src="...jquery.js?ln=primefaces&v=5.0"></script>
...
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>THE TITLE</title>
<link rel="icon" href="resources/img/favicon.ico">
<div id="textarea_simulator" style="position: absolute; top: 0px; left: 0px; visibility: hidden;"></div>
</body>
控制台给出了以下错误:
[Warning] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.
[Error] Uncaught TypeError: Cannot read property 'debug' of undefined
正如您所看到的,除<div id="textarea_simulator">
元素外,它是相同的。当我退出对MB的人物属性的引用时,页面工作正常(我的意思是删除value=#{}
元素中的<p:inputText />
。
根据the video tutorial和JSF wiki( ch 8.4使用Facelets模板,第136页)中建议的Java EE documentation,当您拥有ui:composition
标记( Facelets模板客户端),忽略外部的所有内容。因此,我尝试将XML命名空间定义放在index
和register
的此标记中,但结果是相同的。
My Managed Beans如下:
@ManagedBean
@RequestScoped
public class PersonBean {
@ManagedProperty("#{userBean}")
private UserBean userBean;
private Person person;
//Empty constructor, getter/setter
public void addPerson(){
//In a try/catch block: open connection, make query with prepared statement and commit
}
}
@ManagedBean
@RequestScoped
public class UserBean {
private User user;
private String repeatPassword;
//Constructor, getters and setters
}
最后,Person
和User
是POJO的属性,getter和setter(没有构造函数)我错过了什么?我的错是什么?
我正在使用Netbeans 8.0.2和Glasfish Sever 4.1。非常感谢任何帮助。提前谢谢。
Glassfish服务器给我以下消息:
SEVERE: JSF cannot create the managed bean personBean when it is required. Following errors have been found:
- The property userBean does not exists for the managed bean personBean
(我正在从西班牙语翻译,所以消息可能与您可以获得的消息不同)。我为@ManagedProperty("#{userBean}")
(大写)更改了@ManagedProperty("#{UserBean}")
,但结果是相同的。
答案 0 :(得分:0)
知道了。而不是使用@ManagedProperty
注释
import javax.faces.bean.ManagedProperty;
...
@ManagedProperty("#{userBean}")
private UserBean userBean;
我使用了@Inject
注释
import javax.inject.Inject;
...
@Inject private UserBean userBean;
现在呈现完美。解决方案来自The Java EE Tutorial, Release 7,第23.7章.-注入豆,第23.6页(400)。
我已经看过使用@ManagedProperty
的其他教程/示例并且工作正常,但由于某种原因在这里不起作用。事实上,我在提到的教程中进行了搜索,但@ManagedProperty
没有参考,但它在Java EE API中有记录。