3

I am developing a JSF 2.0 application. On click of , I perform some logic in the backend bean action, need to redirect to another URL in a new window, and perform some clean up work in my original bean action before exiting the action. I am currently using externalContext.redirect() method along with "faces-redirect=true" (since I have some clean up code to be executed after the redirect happens). But the external URL is being opened in the same window.

I would require it to open in a separate new browser window, while the clean up code after redirect is executed too. Can you please let me know how this can be done ?

user1722908
  • 535
  • 2
  • 9
  • 21

1 Answers1

4

You cannot open a new browser window/tab from server side on. This can only be done from client side on. One of the ways is adding the target="_blank" attribute to the HTML form where the submit button is sitting in.

So, in JSF terms, that would be:

<h:form target="_blank">

This has further nothing to do with whatever the backing bean code is doing. It just runs in sync while the server is processing the HTTP request.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I tried your method, but a new window opens even if there are some validation errors. The validation errors are displayed in the new window. I dont want the new window to open if there are any validation errors. Is there any way to do that ? – user1722908 Sep 12 '13 at 17:28
  • Indeed, this was already less or more covered with "One of the ways" in my answer. If you want to take JSF validation into account, then you have to change the way of opening the new window drastically. Basically, you need JavaScript's `window.open()` instead on the given URL. You need to let JSF conditionally render/execute this piece of JS code. See for an elaborate answer with code examples also this related question: http://stackoverflow.com/questions/14584187/how-to-use-target-blank-only-if-validation-successfully – BalusC Sep 12 '13 at 17:31
  • So if we follow method 2 in the post, we would not require to do externalcontext.redirect() am I correct ? – user1722908 Sep 12 '13 at 17:41
  • That's correct. You do not need to navigate at all. Just postback to the same view (return `null` or `void`) and let the conditionally generated JS `window.open()` function call do its job. Note that you can if necessary parameterize `reportURL` with a bean property like so `window.open('#{bean.reportURL}')`. – BalusC Sep 12 '13 at 17:41
  • Thanks . This worked perfectly. However I have another issue here. The web page had rendered a few fields using AJAX and based on certain user actions, before the submit button was clicked. These fields disappear when the new window opens. Any way to retain these fields in the page even after the window.open executes ? – user1722908 Sep 12 '13 at 18:31
  • I also noticed that after the window opens, $(document).ready(function() is automatically called (page is reloaded) and so the ajax rendered fields are hidden. Is there any way to go around this ? – user1722908 Sep 12 '13 at 20:03
  • Make the submit button an ajax button instead. – BalusC Sep 12 '13 at 20:04