JSF2:使用commandLink不会将页面重新加载为commandButton

时间:2012-04-04 15:55:31

标签: java-ee jsf-2 richfaces

我正在尝试通过链接更改语言。使用时工作正常。如果我使用并导航到另一个页面(使用操作),它也可以。当然,我不希望在更改语言时导航到另一个页面。我的理解是commandLink应该与commandButton类似。我尝试了不同的approches(在互联网上找到),但似乎可以让它工作。我在这里错过了什么?

另外我应该补充一下,我正在输入方法(请参阅下面的printIt()),因为我在控制台中看到了跟踪显示。

语言链接位于标题页(header.xhtml)中。我正在使用一个模板(template.xhtml),其中包含header.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是(使用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

1 个答案:

答案 0 :(得分:0)

如果您的侦听器方法有效 - 您可以添加     onclick="window.location.reload" to commandLink属性并通过javascript重新加载页面