2012-04-04 3 views
0

neuladen Ich versuche, die Sprache von über eine Verknüpfung mit zu ändern. Bei der Verwendung funktioniert es gut. Wenn ich eine andere Seite benutze und dorthin navigiere (mit Aktion), funktioniert es auch. Natürlich möchte ich nicht zu einer anderen Seite wechseln, wenn ich die Sprache ändere. Mein Verständnis ist, dass der commandLink ähnlich wie der commandButton funktionieren sollte. Ich habe verschiedene Ansätze ausprobiert (im Internet gefunden), aber ich kann es einfach zum Laufen bringen. Was fehlt mir hier?JSF2: CommandLink wird nicht Seite als commandButton

Auch ich sollte hinzufügen, dass ich die Methode eingeben (siehe printIt() unten), wie ich die Ablaufverfolgung in der Konsole anzeigen.

Der Sprachlink befindet sich in einer Kopfseite (header.xhtml). Ich verwende eine Vorlage (template.xhtml), die die header.xhtml und einen body-Inhalt enthält. Es gibt kein Formular in der template.xhtml.

Außerdem verwende ich JSF 2, richFaces und Bereitstellung auf Tomcat 6, sondern auch auf Tomcat 7. Standard-Browser ist IE9.

Hier ist 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> 

Hier mein template.xhml ist:

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

Hier ist meine Seite:

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

Mein web.xml ist (erzeugt mit Richfaces Archetyp):

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

Und mein Gesicht-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> 

Schließlich mein LocaleChanger ist als:

@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

Ich muss gehen Das gleiche Problem auch. Wenn ich die gleiche Aktion mit einem Befehlslink anstelle einer Befehlsschaltfläche verwende, passiert nichts. Es gibt keinen Aufruf der Bean (kein Konstruktoraufruf oder etwas anderes). Ich denke, dass dieser Befehl einige Phasen überspringt ...:/Wenn ich keine Lösung finden kann, formatiere ich einen commandButton in css wie einen normalen Link ...: / – Tobi

Antwort

0

wenn Ihr Listener-Methode funktioniert - Sie hinzufügen können onclick="window.location.reload" Attribute command und laden Sie die Seite über Javascript