我正在尝试启动一个小型JSF应用程序但没有成功。在Glassfish上尝试过但我遇到了以下错误(Trying to use CDI on Glassfish 4 results in javax.el.PropertyNotFoundException: Target Unreachable, identifier 'indexMB' resolved to null)。
所以我尝试将应用程序迁移到TomEE 1.5.2,但是虽然在控制台中没有显示错误,但页面没有加载JSF组件,如下图所示:
任何帮助都非常有用。
按照我的设置和我的文件:
** index.xhtml
<html ...>
<f:loadBundle basename="i18n" var="bundle" />
<h:head>
<title>#{bundle['index_title']}</title>
</h:head>
<h:body>
#{bundle['index_appname']}
<br />
<h:form id="frmIndex">
<p:panelGrid columns="2">
<p:outputLabel for="user" value="#{bundle['lblUser']}" />
<p:inputText id="user" value="#{indexMB.user}" />
<p:outputLabel for="password" value="#{bundle['lblPassword']}" />
<p:password id="password" value="#{indexMB.password}" />
</p:panelGrid>
<p:commandButton action="#{indexMB.loginTest}" value="#{bundle['btn_login']}" />
</h:form>
</h:body>
** IndexMB.java
@ManagedBean ("indexMB")
@RequestScoped
public class IndexMB {
private String password;
private String user;
public IndexMB() {
}
public String loginTest(){
...
}
// getters and setters
}
** web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
** faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<locale-config>
<default-locale>pt_BR</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
</application>
** beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">
</beans>
答案 0 :(得分:2)
页面未加载JSF组件
页面 加载组件,否则您的Facelets代码中的<input type="text" />
(以及其他组件)不会出现<p:inputText id="user" value="#{indexMB.user}" />
。问题似乎出现在#{bundle['<whatever>']}
配置中。请注意,此问题与GlassFish和TomEE无关,只与国际化配置有关。
对于JSF 2.x,我按照此Q / A中的说明进行了说明:https://stackoverflow.com/a/4830669/1065197以及提供更多信息的链接:Internationalization in JSF with UTF-8 encoded properties files。根据该教程,我做了一个测试应用程序。简而言之:
在faces-config.xml中配置捆绑包
<application>
<locale-config>
<default-locale>pt_BR</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
<!--
Here should be the full name of the package and the
name of the properties files with the i18n text.
Example (from a personal project):
edu.home.store.view.idioma.tienda
Where the properties files are:
edu.home.store.view.idioma.tienda_es.properties
edu.home.store.view.idioma.tienda_en.properties
-->
<!-- <base-name>edu.home.store.view.idioma.tienda</base-name> -->
<!-- assumming your file is directly posted in class folder -->
<base-name>i18n</base-name>
<!--
Name of the variable to use in Facelets files.
-->
<var>bundle</var>
</resource-bundle>
</application>
创建一个@SessionScoped
bean,用于处理您的网页中使用的区域设置。
@ManagedBean
@SessionScoped
public class LocaleBean implements Serializable {
private static final long serialVersionUID = 89794215646544L;
private Locale locale;
public LocaleBean() {
}
@PostConstruct
public void init() {
//give the default value here
locale = new Locale("pt_BR");
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
public Locale getLocale() {
return locale;
}
public String getLenguaje() {
return locale.getLanguage();
}
public void setLenguaje(String lenguaje) {
locale = new Locale(lenguaje);
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
}
使用<f:view>
包裹视图的所有内容并定义要使用的语言。优选地,这应该在主模板中。例如:
<f:view locale="#{localeBean.locale}">
<!-- Your Facelets code goes here... -->
</f:view>
除了这个问题,我强烈建议您将明显自动生成的Faces Servlet
映射配置从/faces/*
更改为*.xhtml
:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
更多信息: