2012-04-04 52 views
0

我想通過鏈接使用更改語言。當使用它工作正常。如果我使用並導航到另一個頁面(使用操作),它也可以使用。當然,我不想在更改語言時導航到另一個頁面。我的理解是,commandLink應該與commandButton類似。我嘗試了不同的方式(在互聯網上找到),但似乎可以讓它起作用。我在這裏錯過了什麼?JSF2:使用commandLink不能重新加載頁面作爲命令按鈕

另外,我應該補充說,我正在輸入方法(請參閱下面的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.xhml:

<!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文件是(產生with richfaces archetype):

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

+0

我走了也是同樣的問題。如果我使用與commandlink相同的動作而不是commandbutton,則不會發生任何事情。沒有調用bean(沒有構造函數調用或別的東西)。我認爲,該命令鏈接跳過了一些階段...:/如果我找不到任何解決方案,我將格式化命令按鈕在CSS像正常鏈接...:/ – Tobi 2012-04-04 19:52:18

回答

0

如果監聽方法的工作 - 你可以添加 onclick="window.location.reload" 到commandLink屬性並通過javascript重新加載頁面

相關問題