我是JSF的新手,我想将我的网页国际化,以便网站访问者能够 通过单击代表每个区域设置的标志图标,可以在语言之间切换。 我编辑了faces-config.xml并添加了资源包,默认语言环境和支持的语言环境。 然后我写了一个名为changeLang.java的bean,它应该用于后台工作。 当页面被评估时,我收到以下错误:
index.xhtml @8,89 locale="#{changeLang.currentLocale}" Attribute did not evaluate to a String or Locale: null
技术:
JSF 2.0 Mojarra 2.0.2
Spring 3.0.0
小面
我
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));
}
}
和“使用网页”代码段
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 com.sun.faces.context.flash.ELFlash@119f705d
拜托,你能帮我解决这个问题吗?
感谢您的任何建议。
<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 :(得分:1)
嗯,您需要设置默认值。任
Locale currentLocale = FacesContext.getCurrentContext()
.getApplication().getDefaultLocale();
或
@PostConstruct
public void init() {
currentLocale = FacesContext.getCurrentContext()
.getApplication().getDefaultLocale();
}
答案 1 :(得分:0)
错误表明Locale
为空,但可能不为空。您需要使用ExternalContext#getRequestLocale()
预先初始化它,这将返回客户端区域设置,而不是Application#getDefaultLocale()
,它将返回服务器端区域设置如果没有明确配置,则为null
。
因此,请使用
更新您的代码public changeLang() {
this.currentLocale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
}
那就是说,建议坚持Sun Java Naming Conventions。类名应该以大写字母开头,否则会让每个人感到困惑,因为它看起来像方法。将changeLang
重命名为ChangeLang
。