includeViewParams = true在模板化页面中不起作用

时间:2012-01-31 14:11:06

标签: java jsf-2 facelets post-redirect-get

考虑这个模板:

<!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">
    <f:view contentType="text/html">
        <ui:insert name="metadata"/>
        <h:head>
            <title></title>
        </h:head>
        <h:body>
            <ui:insert name="content"/>
        </h:body>
    </f:view>
</html>

使用它的页面(/pages/test.xhtml):

<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">
    <f:view contentType="text/html">
        <h:head>
            <title></title>
        </h:head>
        <h:body>
            <ui:composition template="/WEB-INF/templates/testLayout.xhtml">
                <ui:define name="metadata">
                    <f:metadata>
                        <f:viewParam name="foobar" value="#{pageBean.foo}"/>
                    </f:metadata>
                </ui:define>
                <ui:define name="content">
                    <h:form>
                        <h:commandLink value="Click"
                                       action="#{util.currentPageAction()}"/>
                    </h:form>
                </ui:define>
            </ui:composition>
        </h:body>
    </f:view>
</html>

页面的bean:

@Named
@RequestScoped
public class PageBean implements Serializable
{
    public String getFoo()
    {
        return foo;
    }

    public void setFoo(String foo)
    {
        this.foo = foo;
    }

    private String foo;
}

和这个bean:

@Named
@ApplicationScoped
public class Util implements Serializable
{
    public String currentPageAction()
    {
        return FacesContext.getCurrentInstance().getViewRoot().getViewId() +
                   "?faces-redirect=true&includeViewParams=true";
    }
}

当我在浏览器中加载http://localhost:8080/faces/pages/test.xhtml?foo=bar并点击<h:commandLink/>时,网址会更改为http://localhost:8080/faces/pages/test.xhtml。也就是说,视图参数不包含在重定向URL中。

但是,当我重构页面使其不使用模板时,它的行为与预期一致。也就是说,视图参数包含在重定向URL中。

我理解<f:metadata/>在放入facelets模板时不会也不应该工作。这不是这里发生的事情,所以这是一个不同的问题。规范中没有任何内容表明无法做到这一点。实际上,根据我的知识,没有其他方法可以创建一个带有视图参数的基于模板的页面。

1 个答案:

答案 0 :(得分:0)

我有类似的问题,我将其解析为定义“元数据”集元数据在f:view

之后立即注入

<ui:define name="metadata">
   <f:metadata>
           <f:viewParam name="id" .../>
   </f:metadata>
    </ui:define>

的template.xhtml

        <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
       xml:lang="en" lang="en">
         <f:view>
    <ui:insert name="metadata"/>
       <div id="container">
    <ui:insert name="content"/>
            </div>
          </f:view>
相关问题