csvan
csvan

Reputation: 9454

Getting page parameters for a requestscoped bean in a form

I have the following page:

<?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>

The backing bean used above is RequestScoped, and the page itself takes a single parameter (sessionKey)...what I want to do is to: 1. Bind sessionKey to a variable in the backing bean. This is straightforward enough. 2. Use the bound value of sessionKey when executing dedicated logic in the same bean, when the client presses the commandButton.

The problem is that pressing the button starts a new request, which invalidates both the current bean (with the bound value), as well as the external page context...I thus lose all means to get a hold of sessionKey from either the bean or the page parameters...how can I resolve this? I am relatively new to both web programming and JSF, so pardon me if this has an obvious answer.

Upvotes: 0

Views: 1143

Answers (1)

BalusC
BalusC

Reputation: 1108632

Either put the bean in the view scope, so that it lives long as you're interacting with the same view, or pass the request parameter by <f:param> to the subsequent requests.

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

By the way, you'd rather have used <f:viewParam> to bind the request parameter to the bean directly.

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

See also:

Upvotes: 2

Related Questions