【问题标题】:Redirect in managed bean not working托管 bean 中的重定向不起作用
【发布时间】:2013-07-01 11:25:46
【问题描述】:

我在使用 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?

我完全不明白,对不起!这可能是一件微不足道的事情,但我无法解决它!

问候

【问题讨论】:

  • 您是说页面是在没有模板的情况下呈现的,还是根本不呈现?还有你 web.xml 中的 url 模式是什么?
  • 它被渲染但没有模板! URL 模式如下所示:&lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;!
  • 您的页面可能看不到模板。检查路径,直接加载页面看看。
  • 但是为什么使用 layoutTempl 可以正确呈现索引页面?当我在 abcContent.xhtml.. 之前添加 faces/ 时,页面会正确呈现,但 GlassFish 服务器现在会抛出警告 WARNING: JSF1015: Request path '/faces/abcContent.xhtml' begins with one or more occurrences of the FacesServlet prefix path mapping '/faces'
  • 为什么在abcContent.xhtml 中使用&lt;body&gt; 标签?

标签: jsf primefaces


【解决方案1】:

试试

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

重定向不需要 ajax。

【解决方案2】:

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

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

【讨论】:

  • 所有文件都在同一个目录中,删除 ./ 似乎也没有帮助,对不起!
  • 能不能把bean中的.xthml去掉,只留下页面名。
【解决方案3】:

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>

没有&lt;ui:composition&gt;。您正在使用 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>

【讨论】:

    【解决方案4】:

    这是一个较晚的响应,但根据我的经验,当文件路径为时会发生这种情况

    “abcContent.xhtml”

    而不是

    “面孔/abcContent.xhtml”

    您甚至可以在浏览器的地址栏中对其进行测试。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2011-09-02
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多