我正在尝试通过使用 . 使用时效果很好。如果我使用并导航到另一个页面(使用操作),它也可以工作。自然,我不想在更改语言时导航到另一个页面。我的理解是 commandLink 的工作方式应该与 commandButton 类似。我尝试了不同的方法(在互联网上找到),但似乎可以让它工作。我在这里想念什么?
我还应该补充一点,当我看到控制台中的跟踪显示时,我正在输入方法(请参见下面的 printIt())。
语言链接位于标题页 (header.xhtml) 中。我正在使用包含 header.xhtml 和正文内容的模板 (template.xhtml)。template.xhtml 中没有表单。
此外,我正在使用 JSF 2、richFaces 并在 tomcat 6 上进行部署,但也在 tomcat 7 上进行了尝试。默认浏览器是 IE9。
这是header.xhtml:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<div class="Header">
<div class="Headerleft">
<h:graphicImage library="images" name="logo.png" />
</div>
<div class="HeaderTopNav">
<f:view>
<h:form id="header">
<h:commandButton actionListener="#{localeChanger.printIt}" value="kkk" />
<h:commandLink actionListener="#{localeChanger.printIt}" value="jjj" />
| <a href="#">#{msg['header.about']}</a>
| <a href="#">#{msg['contact.webmaster']}</a>
</h:form>
</f:view>
</div>
</div>
</ui:composition>
这是我的template.xml:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<f:view locale="#{localeChanger.locale}">
<h:head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="x-ua-compatible" content="IE=8" />
<title>#{msg['header.title']}</title>
<h:outputStylesheet library="css" name="style.css" />
<h:outputStylesheet library="css" name="jquery.css" />
<h:outputScript library="js" name="jquery-1.7.1.js" target="head" />
<h:outputScript library="js" name="jquery.cookie.js" target="head" />
<h:outputScript library="js" name="jquery.ui.core.js" target="head" />
<h:outputScript library="js" name="jquery.ui.widget.js" target="head" />
<h:outputScript library="js" name="jquery.ui.tabs.js" target="head" />
<script>
$(function() {
$( "#tabs" ).tabs({
cookie: {
// store cookie for a day, without, it would be a session cookie
expires: 1
}
});
});
</script>
</h:head>
<h:body>
<div id="Wrap">
<div class="MainContent">
<!-- HEADER -->
<div id="header">
<ui:insert name="header">
<ui:include src="/templates/cie/header.xhtml" />
</ui:insert>
</div>
<div class="container">
<ui:insert name="body" />
</div>
</div>
</div>
</h:body>
</f:view>
</html>
这是我的页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j">
<body>
<ui:composition template="/templates/cie/template.xhtml">
<ui:define name="title">RichFaces Sample</ui:define>
<ui:define name="body">
<h:form prependId="false">
<h:outputLabel value="Name:" for="nameInput" />
<h:inputText id="nameInput" value="#{richBean.name}">
<a4j:ajax event="keyup" render="output" />
</h:inputText>
<h:panelGroup id="output">
<h:outputText value="Hello #{richBean.name}!"
rendered="#{not empty richBean.name}" />
</h:panelGroup>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
我的 web.xml 是(使用 Richfaces 原型生成):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<display-name>Sample RichFaces 4 Application</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.SKIP_COMMENTS</param-name>
<param-value>true</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>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- Resource Servlet - serves static resources and resources for specific components -->
<servlet>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>org.richfaces.webapp.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/org.richfaces.resources/*</url-pattern>
</servlet-mapping>
<!-- Resource Mapping - resources will be served compressed and packed in production -->
<context-param>
<param-name>org.richfaces.resourceMapping.enabled</param-name>
<param-value>true</param-value>
</context-param>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
还有我的面部配置:
<?xml version="1.0"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" 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-facesconfig_2_0.xsd">
<application>
<locale-config>
<default-locale>fr_CA</default-locale>
</locale-config>
<resource-bundle>
<base-name>ca.cie.template.msgs.messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
最后我的 LocaleChanger 是:
@ManagedBean
@SessionScoped
public class LocaleChanger implements Serializable, ActionListener {
private static final long serialVersionUID = 1L;
final Logger logger = LoggerFactory.getLogger(LocaleChanger.class);
public void setLocale(String locale1) {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(getLocaleFromString(locale1));
}
public Locale getLocale() {
return FacesContext.getCurrentInstance().getViewRoot().getLocale();
}
public String i18nAction() {
System.out.println("Hello JCL...");
FacesContext context = FacesContext.getCurrentInstance();
logger.info("i18nAction Locale={}", context.getViewRoot().getLocale().toString());
Locale lang = Locale.CANADA;
if (context.getViewRoot().getLocale() == lang) {
lang = Locale.CANADA_FRENCH;
}
context.getViewRoot().setLocale(lang);
return lang.toString();
}
private static Locale getLocaleFromString(String localeString) {
if (localeString == null) {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().getLocale();
}
localeString = localeString.trim();
if (localeString.toLowerCase().equals("default")) {
return Locale.getDefault();
}
// Extract language
int languageIndex = localeString.indexOf('_');
String language = null;
if (languageIndex == -1) {
// No further "_" so is "{language}" only
return new Locale(localeString, "");
} else {
language = localeString.substring(0, languageIndex);
}
// Extract country
int countryIndex = localeString.indexOf('_', languageIndex + 1);
String country = null;
if (countryIndex == -1) {
// No further "_" so is "{language}_{country}"
country = localeString.substring(languageIndex + 1);
return new Locale(language, country);
} else {
// Assume all remaining is the variant so is
// "{language}_{country}_{variant}"
country = localeString.substring(languageIndex + 1, countryIndex);
String variant = localeString.substring(countryIndex + 1);
return new Locale(language, country, variant);
}
}
public void printIt(ActionEvent event){
System.out.println("This is an action JCL...");
i18nAction();
}
@Override
public void processAction(ActionEvent event) throws AbortProcessingException {
System.out.println("In process ActionEvent?");
i18nAction();
}
}
jc