2012-04-04 8 views
0

私は、を使用してリンク経由で言語を変更しようとしています。それを使用すると正常に動作します。私が別のページを使用して(アクションを使用して)使用すると、それも機能します。もちろん、私は言語を変更するときに別のページに移動したくない。 CommandLinkはcommandButtonと同様に動作するはずです。私は(インターネット上で見つけた)異なるアトラクションを試しましたが、うまく動作するように思えます。私はここで何が欠けていますか?JSF2:コマンドリンクを使用してページをコマンドボタンとしてリロードしていません。

また、コンソールにトレース表示が表示されるので、私はメソッド(後述の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を(生成されますリッチフェイスのアーキタイプ付き):

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

そして、私の顔-config設定:

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

私は行った同じ問題も同じです。コマンドボタンの代わりにコマンドリンクで同じアクションを使用すると、何も起こりません。 Beanへの呼び出しはありません(コンストラクタ呼び出しも何もありません)。私はcommandlinkがいくつかの段階をスキップすると思います:/もし私が解決策を見つけることができないなら、私は通常のリンクのようにCSSでcommandButtonをフォーマットします:... / – Tobi

答えて

0

- あなたはのcommandLink属性に onclick="window.location.reload" を追加することができますJavaScriptを使用してページをリロードする

関連する問題