-11

After performing the appropriate action, the method of my bean must ensure that the browser back to the previous page. How can I handle it?

I am using the CDI Conversation.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
BillB
  • 37
  • 1
  • 10

2 Answers2

7

Pass the current URI as request parameter along during navigation to the page containing that action.

<h:link value="Go to page containing that action" outcome="pageContainingThatAction.xhtml">
    <f:param name="from" value="#{request.requestURI}" />
</h:link>

(use #{view.viewId} instead if you want to pass the view ID)

Set and remember that parameter representing the URI in the view/conversation scoped managed bean.

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

Finally redirect to that URI in that action method.

public void thatActionMethod() throws IOException {
    // ...

    externalContext.redirect(from);
}
George Stocker
  • 57,289
  • 29
  • 176
  • 237
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

After asking you a couple of question in comments, I think what you want to do is :

  • On your button, specify the attribute action to a function in your bean that return a String.
  • The String returned need to be the navigation path to the page you want to be redirected.
  • If the validation is wrong on button click, then simply return null and it will stay on same page.
  • Make sure you define the proper navigation rule in faces-config.xml

See this tutorial for how to configure navigation rules.

See Primefaces commandButton doc for info on action tag.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76