2013-10-03 65 views
1

我試圖啓動一個小型的JSF應用程序,但沒有成功。嘗試過Glassfish,但我有以下錯誤(Trying to use CDI on Glassfish 4 results in javax.el.PropertyNotFoundException: Target Unreachable, identifier 'indexMB' resolved to null)。JSEE頁面未載入TomEE 1.5.2

所以,我想給應用程序遷移到TomEE 1.5.2,不過雖然顯示在控制檯中沒有錯誤,頁面沒有加載的JSF組件,你可以在下面的圖片中看到:

JSF not loade

任何幫助將是非常有用的。

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

  • TomEE 1.5.2
  • Primefaces 3.5
  • 公地文件上傳1.3
  • 公地IO 2.4

**指數。 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> 

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

回答

2

頁面沒有加載的JSF組件

加載組件,否則不會有一個<input type="text" />是來自Facelets代碼中的<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。在那篇教程的基礎上我做了一個測試應用程序總之:

  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. 創建@SessionScoped豆將處理在你的頁面中使用的語言環境。

    @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/*的明顯自動生成Faces Servlet映射配置更改爲*.xhtml

<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>*.xhtml</url-pattern> 
</servlet-mapping> 

更多信息:

+2

請注意,PrimeFaces CSS也未應用。你的回答沒有涵蓋。 – BalusC

+0

@ luiggi-mendoza,謝謝你的回答。這個servlet映射是由Netbeans自動生成的,我會根據你的建議改變(我現在不能測試,稍後再做)。 –

+0

@BalusC,我需要更改爲CSS工作? –