获取表单中的requestcoped bean的页面参数

时间:2012-10-25 01:59:56

标签: jsf jsf-2

我有以下页面:

<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:ui="http://java.sun.com/jsf/facelets">



<h:head>
    <title>Fire - Registration</title>
</h:head>

<h:body>

    <f:view>
        <f:event type="preRenderView" listener="#{confirmPasswordResetBean.bindSessionKey()}"/>
    </f:view>

    <p:growl id="growl" showDetail="true" life="5000" />

    <h:form>

        <p:panel header="Select your new password">

            <h:panelGrid columns="2" cellpadding="5">

                <p:outputLabel for="newPassword" value="Type your new password"/>
                <p:password id="newPassword" 
                            value="#{confirmPasswordResetBean.firstPassword}"
                            feedback="true"/>

                <p:outputLabel for="retypedPassword" value="Retype your password"/>
                <p:password id="retypedPassword" 
                            value="#{confirmPasswordResetBean.secondPassword}"/>

            </h:panelGrid>

            <p:commandButton id="confirmButton" 
                             value="reset" 
                             action="#{confirmPasswordResetBean.doResetPassword()}"
                             update=":growl"/>

        </p:panel>       

    </h:form>

</h:body>

上面使用的支持bean是RequestScoped,页面本身只接受一个参数(sessionKey)...我想要做的是:  1.将sessionKey绑定到辅助bean中的变量。这很简单。  2.当客户端按下commandButton时,在同一个bean中执行专用逻辑时,使用sessionKey的绑定值。

问题是按下按钮会启动一个新请求,这会使当前bean(带有绑定值)以及外部页面上下文无效...因此,我失去了从中获取sessionKey的所有方法无论是bean还是页面参数......我该如何解决这个问题?我对网络编程和JSF都比较陌生,所以请原谅我,如果这有明显的答案。

1 个答案:

答案 0 :(得分:2)

将bean放在视图范围内,以便在您与同一视图交互时将其存在很长时间,或者将<f:param>的请求参数传递给后续请求。

<p:commandButton ...>
    <f:param name="sessionKey" value="#{param.sessionKey}" />
</p:commandButton>

顺便说一句,您宁愿使用<f:viewParam>直接将请求参数绑定到bean。

<f:metadata>
    <f:viewParam name="sessionKey" value="#{bean.sessionKey}" />
</f:metadata>

另见: