[英]How does the POST REDIRECT GET works in JSF on commandButton
我正在測試JSF的幾個新功能,我遇到了Post Redirect Get。 我想從我的第一頁重定向說first.xhtml到second.xhtml。
我在兩個托管bean中都有一個數字作為屬性,我想使用request參數將它從第一個bean傳遞給第二個bean。
這是我的第一頁
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Landing Page</title>
</head>
<body>
<h3>Enter Number</h3>
<h:form>
<h:inputText id="input" name="number" value="#{postRedirectGet.number}" />
<h:commandButton value="redirect to result"
action="resultPage?faces-redirect=true&includeViewParams=true">
</h:commandButton>
</h:form>
</body>
</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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:metadata>
<f:viewParam name="number" value="#{postRedirectResult.number}"/>
</f:metadata>
<head>
<title>Result Page</title>
</head>
<body>
<h:form>
<h:outputText value="Number #{postRedirectGet.number}" />
<h:outputText value="Number #{postRedirectResult.number}" />
<h:commandButton value="Redirect to index" action="/index?faces-redirect=true" />
</h:form>
</body>
</html>
現在頁面正在使用commandButton進行POST,然后從第一頁重定向到第二頁,但它在URL中傳遞number = 0。 如果我改變它就有效
<f:viewParam name="number" value="#{postRedirectResult.number}"/>
至
<f:viewParam name="number" value="#{postRedirectGet.number}"/>
但我認為viewParam用於將值設置為bean,而不是用於傳遞URL中的值。 有人可以解釋我們如何進行POST並在下一頁設置托管bean的屬性。
問題是f:viewParam
在兩種情況下以兩種不同的方式使用。 在h:link中,它用於設置目標bean的屬性,在h:commandButton
它用於計算GET URL,然后可以使用@ManagedProperty
設置目標bean屬性。 我的理解是正確的還是可以使用f:viewParam
來設置h:commandButton
的屬性h:commandButton
POST redirect get also。
您似乎缺少的是includeViewParams
功能。 讓我引用這篇內容非常豐富的文章(你應該閱讀所有文章):
另一個特殊查詢字符串參數includeViewParams告訴導航處理程序在執行導航時包含視圖參數。 但是應該包含哪些視圖參數? 執行導航時要包括的視圖參數在to-view-id頁面上聲明。
因此,JSF會查看您的resultpage.xhtml
以確定要傳遞的參數。 然后盡職地繼續傳遞postRedirectResult#number
的當前值(此時未設置/ 0
)。
要在您的bean中反映GET number
參數,請將其作為真實參數傳遞:
<h:commandButton value="redirect to result"
action="resultPage?faces-redirect=true&number=4" />
也可以看看:
有四種方法可以將數據從JSF頁面傳輸到Backing Bean。 我們可以用
f:param
f:setPropertyActionListener
f:attribute
在這里你可以嘗試f:setPropertyActionListener
as ..
<h:commandButton value="redirect to result"
action="resultPage?faces-redirect=true">
<f:setPropertyActionListener target="#{postRedirectResult.number}" value=4 />
</h:commandButton>
這里是鏈接此 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.