1

我正在尝试启动一个小型 JSF 应用程序,但没有成功。在 Glassfish 上尝试过,但出现以下错误(尝试在 Glassfish 4 上使用 CDI 导致 javax.el.PropertyNotFoundException: Target Unreachable, identifier 'indexMB' resolved to null)。

所以我尝试将应用程序迁移到 TomEE 1.5.2,但是虽然在控制台中没有显示错误,但页面没有加载 JSF 组件,如下图所示:

JSF 未加载

任何帮助都会非常有用。

按照我的设置和我的文件:

  • 汤姆EE 1.5.2
  • Primefaces 3.5
  • 公共文件上传 1.3
  • commons-io 2.4

** 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>

** 索引MB.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>

** 面孔-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>

** 豆类.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>
4

1 回答 1

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和提供更多信息的链接:JSF 中的国际化与 UTF-8 编码的属性文件。基于该教程,我制作了一个测试应用程序。简而言之:

  1. 在 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>
    
  2. 创建一个@SessionScopedbean 来处理要在页面中使用的语言环境。

    @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);
        }
    }
    
  3. <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>

更多信息:

于 2013-10-03T03:10:13.087 回答