0

This should be simple, but I am looking at other questions and I am not able to find the right answer for my issue.

I have a JSF page that calls a method myController.load():

<f:metadata>
     <f:viewParam name="id" value="#{myController.id}" required="false"/>
     <f:viewAction action="#{myController.load()}" />

This method will generate an id that is placed in myController.id if originally the page was not called with such parameter. My question is how can I make the URL at the navigation bar reflect this change, i.e. insert this new parameter in the URL. Basically:

--> Browse to myPage.xhtml

--> call to myController.load() which sets myController.id = 1

--> Reflect in URL myPage.xhtml?id=1. Ideally without re-loading the page

user1156544
  • 1,725
  • 2
  • 25
  • 51
  • what answers are you looking at then? – Kukeltje Jul 18 '18 at 12:46
  • I meant that I am trying to find a solution in some other answers, (e.g. https://stackoverflow.com/questions/4994458/how-can-i-pass-selected-row-to-commandlink-inside-datatable-or-uirepeat) but none of them seem to address my problem – user1156544 Jul 18 '18 at 15:08

1 Answers1

1

You will want to do some reading about PRG (Post/Redirect/Get). Here is a good start.

https://mobiarch.wordpress.com/2012/08/09/doing-post-redirect-get-pattern-in-jsf-2/

You'll want your original JSF link to call an ActionListener that will create a scoped reference to myController and set the id attribute to 1 there... then you can use PRG to redirect to your page. PRG will use the value of your bean to build the new URL correctly as you want.

This ActionListener method is very simple just to help illustrate...

// my ActionListener method
public String goToMyPage() {
    myController.setId(1); // assuming myController is declared in scope.

    return "myPage?faces-redirect=true&includeViewParams=true";
}

Hope this helps get you started.

Clint Munden
  • 334
  • 1
  • 6
  • @user1156544 But please note, that "abusing" redirects to get the desired links is basically a real "killer" of the idea on how the framework is supposed to work. Originating from the "PHP/HTML"-World i was trying to "mime" this behaviour as well... It turned out to be a very bad idea, because you throw away all the benefits of partial-page-loading (if you redirect after every click)... A common way to "bypass" this, is to abuse the "anchor" tag (which can be modified with simple javascript): i.e. "my.domain.com/#id=5&mode=test" rather than *real* parameters. – dognose Jul 26 '18 at 18:05
  • In this case I think it is OK since I want to actually go to another page. In fact, I've been quite silly since I have done this in the past, and I just didn't remember. – user1156544 Jul 26 '18 at 22:02