托管bean中的重定向无法正常工作

时间:2013-07-01 11:25:46

标签: jsf primefaces

我遇到了JSF测试应用程序和PrimeFaces组件的问题,但找不到错误。

我开始使用由

定义的模板文件(layoutTempl.xhtml)
....
<h:body>
    <p:layout fullPage="true" >
        <p:layoutUnit position="north" size="95" >
            <ui:insert name="menubar" >
                MenuBar
            </ui:insert>          
        </p:layoutUnit>          
        <p:layoutUnit position="west" resizable="false" size="250">
            <ui:insert name="tree" >
                ProjectTree
            </ui:insert>             
        </p:layoutUnit>
        <p:layoutUnit position="center">
            <ui:insert name="main" >
                MainContent
            </ui:insert>           
        </p:layoutUnit>
        <p:layoutUnit position="south" size="45">
            <ui:insert name="footer" >
                Footer
            </ui:insert> 
        </p:layoutUnit>
    </p:layout>       
</h:body>
....

此模板用于两个页面(indexContent.xhtml):

....
<body>
    <ui:composition template="./layoutTempl.xhtml">
        <ui:define name="menubar">
            MenuBar
        </ui:define>
        <ui:define name="tree">
            Project Tree
        </ui:define>
        <ui:define name="main">
            <ui:include src="index.xhtml"/>
        </ui:define>
        <ui:define name="footer">
            Footer
        </ui:define>
    </ui:composition>
</body>
....

和(abcContent.xhtml):

....
<body>
    <ui:composition template="./layoutTempl.xhtml">
        <ui:define name="menubar">
            MenuBar
        </ui:define>
        <ui:define name="tree">
            Project Tree
        </ui:define>
        <ui:define name="main">
            <ui:include src="abc.xhtml"/>
        </ui:define>
        <ui:define name="footer">
            Footer
        </ui:define>
    </ui:composition>       
</body>
....

包含的文件index.xhtml包含:

....
<h:body>
    <ui:composition >
    Hello from BareTest
    <br /><br />
    <h:form id="myform">
        <p:selectOneMenu id="scroll2"
                         value="#{listTestBean.selectedMyObject}" >
            <f:selectItems value="#{listTestBean.myObjects}"/>
            <p:ajax event="change" listener="#{listTestBean.valueChanged}"/>
        </p:selectOneMenu>
        <br/><br/>
    </h:form>
    </ui:composition>
</h:body>
....

而abc.xhtml包含:

....
<h:body>
    <h2>We got at abc page!</h2>
    <br /><br />
    <h:form id="abcForm">
        <p>#{listTestBean.selectedMyObject}</p>
        <p:commandLink id="Ajax" ajax="true" action="indexContent?faces-redirect=false">  
            <h:outputText value="Main page (link)" />
        </p:commandLink>
    </h:form>
</h:body>
....

请求范围的托管bean listTestBean包含getter和setter方法以及valueChanged方法。 valueChanged方法成立:

....
try {
        FacesContext.getCurrentInstance().getExternalContext().redirect("abcContent.xhtml");
} catch (IOException ioe) {
    System.err.println("listTestBean.valueChanged: IOException");
}
....

基本上是重定向到abcContent页面。

但是,当我从selectItem组件中选择一个项目时,abcContent.xhtml页面没有呈现,具有指定的layoutTempl?

我根本不懂,对不起!这可能是微不足道的,但我无法解决它!

此致

4 个答案:

答案 0 :(得分:0)

<p:commandLink ajax="false" action="indexContent?faces-redirect=false"> 

重定向不需要ajax。

答案 1 :(得分:0)

请输入模板的完整相对网址,而不是template="./layoutTempl.xhtml"。这意味着从当前目录加载模板。如果模板和页面位于同一目录中,只需将模板名称删除./ 看看这个模板教程:

http://www.mkyong.com/jsf2/jsf-2-templating-with-facelets-example/

答案 2 :(得分:0)

abcContent.xhtml你有

<ui:define name="main">
    <ui:include src="abc.xhtml"/>
</ui:define>

abc.xhtml定义为

<h:body>
    <h2>We got at abc page!</h2>
    <br /><br />
    <h:form id="abcForm">
     ...
    </h:form>
</h:body>

没有<ui:composition>。您正在使用XHTML,如果您提供了错误的标记,那么JSF将无法正确解释。如果您使用的是IDE,则在使用ui:include时不会收到警告。您可以通过打开浏览器并单击abcContent.xhtml上的查看源代码来自行验证。您会注意到页面的某些部分仍然包含JSF代码(例如h:body)。修复abc.xhtml然后重试。

<h:body>
    <ui:composition>
        <h2>We got at abc page!</h2>
        <br /><br />
        <h:form id="abcForm">
         ...
        </h:form>
    </ui:composition>
</h:body>

答案 3 :(得分:0)

这是一个迟到的回复,但根据我的经验,当文件路径为

时会发生这种情况

“abcContent.xhtml”

而不是

“的面孔/ abcContent.xhtml”

你甚至可以在浏览器的地址栏中测试它。

此外,这并不意味着您的文件位于名为“faces”的文件夹中。它只是web.xml中指定的url模式

相关问题