f:当commandButton导航到页面时,viewAction被忽略

UNI*_*orn 4 jsf tomcat7 jsf-2.2

我有JSF 2.2功能的问题<f:viewAction action="#{...}/>.我把这个标签放在我的XHTML-Page中,它被称为viewActionStart.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<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:p="http://primefaces.org/ui">
<h:head />
<h:body>
    <f:view>
        <f:metadata>
            <f:viewAction action="#{redirect.getView()}" onPostback="true"
                immediate="true" />
        </f:metadata>
    </f:view>
    <h:outputText value="If you see that text, the viewAction was not performed." />
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

如果我直接访问此XHTML页面,一切正常:redirect.getView()调用该方法并执行重定向到另一个页面.

现在我有了我的登陆页面,它显示在viewActionStart-Page之前.有一个按钮,基本上应该导航到我的viewActionStart-Page来显示<f:viewAction>效果.以下代码来自此着陆页index.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<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:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
    <f:view>
        <h:form>
            <h:commandButton action="viewActionStart.xhtml"
                value="To f:viewAction Page" />
        </h:form>
    </f:view>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

如果我点击该命令按钮,则会显示viewActionStart.xhtml的内容.为什么JSF忽略了<f:viewAction>?我不明白为什么viewAction每个页面加载不起作用.我尝试了那些onPostback和"直接"属性,但没有任何改变.

另一个奇怪的结果是,在我点击命令按钮并查看viewActionStart.xhtml的内容之后,URL仍然保留在localhost:8080/UIIncludeTest/index.xhtml上.但是,在命令按钮导航后,不应该像localhost:8080/UIIncludeTest/viewActionStart.xhtml这样查看这个URL 吗?

难道我做错了什么?或者我只是误解了<f:viewAction>?奇怪的是,如果我直接浏览viewActionStart.xhtml它会起作用.

我正试图让这个工作:

  • JSF-2.2.6
  • Tomcat的7.0.47
  • PrimeFaces 5.0

我已经阅读了很多相关的帖子,<f:viewAction>但没有任何帮助.

Bal*_*usC 6

<f:viewAction onPostback="true">仅适用于同一视图上的回发.也就是说,只有在<h:form>同一个视图中提交时才会运行.将<f:viewAction>永远不会intented经一个提交的触发POST导航要执行<h:form>另一种看法.此外,属性名称中的术语"回发"也证实了这一点.这个术语在Web开发世界中意味着"对页面本身的URL的POST请求".

您需要通过GET请求而不是POST请求打开它.更换<h:commandButton><h:button>:

<h:button outcome="viewActionStart.xhtml" value="To f:viewAction Page" />
Run Code Online (Sandbox Code Playgroud)

(注意:<h:form>不再需要了)

或者,如果你真的打算首先执行一个POST请求,原因是一些不明原因(如果你调用一个bean动作方法会更容易理解,但是你只是在这里导航,所以整个POST请求都没有任何意义),然后让它发送重定向:

<h:commandButton action="viewActionStart.xhtml?faces-redirect-true" value="To f:viewAction Page" />
Run Code Online (Sandbox Code Playgroud)

无论哪种方式,现在都应该在地址栏中正确反映URL.这本身确实是一个强烈的暗示,你是错误的东西;)

也可以看看: