2010-03-02 162 views
2

我是JSF的新手,我想將我的網頁國際化,以便網站訪問者通過點擊代表每個語言環境的標誌圖標,可以在各種語言之間進行切換。 我編輯了我的faces-config.xml並添加了資源包,默認語言環境和支持的語言環境。 然後我寫了一個叫做changeLang.java的bean,它應該是做後臺工作的。 當頁面evaulated我得到以下錯誤:JSF語言環境例外

index.xhtml @8,89 locale="#{changeLang.currentLocale}" Attribute did not evaluate to a String or Locale: null 

技術:
JSF 2.0鑽嘴魚科2.0.2
春3.0.0
Facelets的


changeLang.java文件

 


package gui; 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.Locale; 
import javax.annotation.ManagedBean; 
import javax.faces.context.FacesContext; 
import org.springframework.context.annotation.Scope; 

@ManagedBean 
@Scope("session") 

public class ChangeLang { 
    private Locale currentLocale; 

    public ChangeLang() { 
      this.currentLocale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale(); 
    } 
    public Locale getCurrentLocale() { 
     return this.currentLocale; 
    } 
    public ArrayList getSupportedLocales(){ 
     ArrayList listOfSupp = new ArrayList(); 
     Iterator locales = FacesContext.getCurrentInstance().getApplication().getSupportedLocales(); 
     while(locales.hasNext()) { 
      Locale locale = locales.next(); 
      listOfSupp.add(locale); 
     } 

    return listOfSupp; 
    } 
    public void setCurrentLocaleLanguage(String language) { 
     FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(language)); 
    } 
} 
 

and「using page」snippet


f:view locale="#{changeLang.currentLocale}" contentType="text/html; charset=utf-8" 

完整的日誌:

 

An Error Occurred: 
/index.xhtml @8,89 locale="#{changeLang.currentLocale}" Attribute did not evaluate to a String or Locale: null 
- Stack Trace 
javax.faces.view.facelets.TagAttributeException: /index.xhtml @8,89 locale="#{changeLang.currentLocale}" Attribute did not evaluate to a String or Locale: null 
    at com.sun.faces.facelets.tag.jsf.ComponentSupport.getLocale(ComponentSupport.java:218) 
    at com.sun.faces.facelets.tag.jsf.core.ViewHandler.apply(ViewHandler.java:119) 
    at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:160) 
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:86) 
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:75) 
    at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:145) 
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:716) 
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) 
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) 
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) 
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) 
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) 
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) 
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170) 
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88) 
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76) 
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53) 
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57) 
    at com.sun.grizzly.ContextTask.run(ContextTask.java:69) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) 
    at java.lang.Thread.run(Thread.java:619) 
- Component Tree 

- Scoped Variables 
Request ParametersName Value 
None 

View AttributesName Value 
None 

Request AttributesName Value 
None 

Flash AttributesName Value 
None 

Session AttributesName Value 
None 

Application AttributesName Value 
csfcff [email protected] 
 

請,你會這麼好心幫我解決這個問題?

感謝您的任何建議。

<locale-config> 
     <default-locale>en</default-locale> 
     <supported-locale>en</supported-locale> 
     <supported-locale>sk</supported-locale> 
     <supported-locale>ko</supported-locale> 
    </locale-config> 
+0

給你的faces-config(語言環境部分) – Bozho 2010-03-03 10:40:36

回答

1

那麼,你需要設置默認值。無論是

Locale currentLocale = FacesContext.getCurrentContext() 
      .getApplication().getDefaultLocale(); 

@PostConstruct 
public void init() { 
    currentLocale = FacesContext.getCurrentContext() 
      .getApplication().getDefaultLocale(); 
} 
+0

他已經在構造函數中完成了它。問題在於由於命名約定很差,構造函數看起來不像構造函數。另一個問題是,它返回的服務器端語言環境可能會返回null,如果沒有明確設置/配置。 – BalusC 2010-03-02 23:44:55

+0

啊,沒錯,他說他已經在faces-config中設置了默認語言環境..但讓我們來看看它。 – Bozho 2010-03-03 10:40:01

0

錯誤說,Locale爲空,而它不能爲null。您需要使用ExternalContext#getRequestLocale()進行預初始化,這將返回客戶端區域設置,而不是Application#getDefaultLocale(),如果未明確配置,將返回服務器端區域設置,該區域設置可能是null

所以,用

public changeLang() { 
    this.currentLocale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale(); 
} 

這就是說更新您的代碼,它是建議堅持的Sun Java Naming Conventions。類名應該以大寫開頭,否則會混淆每個人,因爲它看起來像方法。將changeLang重命名爲ChangeLang

+0

好吧,我大寫了類名的第一個字母,然後改變了構造函數,但我仍然得到相同的錯誤。 – init 2010-03-03 09:12:32