0

I want to have a Link and when the user clicks on it it should do the following:

  • Saving the a key into the session
  • Open a new Window with another JSF page
  • It should NOT reload the current page

I don't know how to solve the problem.
When I save the key and open the new Window, the key is empty.
Maybe the process of saving the key is too slow?
And how to prevent the page from reloading?

This is my current code:

JavaScript

function openWin2(url)
{
     var w = 800;
     var h = window.innerHeight - 100;
     var left = ((screen.width-w)/2);
     var top = ((screen.height-h)/2);
     window.open(url, 'Tax', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}

xhtml

<ui:repeat value="#{main.list}" var="items">
    <h:commandLink action="#{main.setClickedId(items.itemId)}">
        <table onclick="openWin2('immobilie.xhtml')"><tr><td>Hello</td></tr></table>
    </h:commandLink>
</ui:repeat>

ManagedBean
Save

public void setClickedId(String clickedId) {
    this.clickedId = clickedId;

    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, Object> map = context.getExternalContext().getSessionMap();
    if(map.containsKey("id")){map.remove("id");}
    map.put("id", clickedId);
}

Load

FacesContext context = FacesContext.getCurrentInstance();
immoId = context.getExternalContext().getSessionMap().get("id").toString();

3 Answers3

0

I would recommend using a4j:ajax tag (available in RichFaces, documentation here). Then the markup in xhtml should look like this:

<h:commandLink action="#{main.setClickedId(items.itemId)}">
    <table><tr><td>Hello</td></tr></table>
    <a4j:ajax oncomplete="openWin2('immobilie.xhtml')" />
</h:commandLink>

This piece of code will send an AJAX request to the server, which will execute method setClickedId, and when the response returns to the browser, the function openWin2 will be called.

Struchu
  • 76
  • 2
  • 8
0

When I save the key and open the new Window, the key is empty. Maybe the process of saving the key is too slow?

The problem here is that JavaScript and server side methods run at different places:

  • JavaScript code runs in client browser, it's like executing code locally in your pc.

  • Server side code runs on server. The browser must do a request to the server, the server will do its job and execute the method defined in the action of your <h:commandLink> and then return a response to the browser. The browser will handle the response. NOTE: this is a very basic explanation, for more info about JSF requests read JSF Lifecycle and Custom components

Knowing this, is not that the method execution is too slow, it's executed at a different time.

And how to prevent the page from reloading?

In short, you must do an ajax request to the server instead of a full request. To accomplish this, JSF 2 offers you <f:ajax> that will convert a simple request into an ajax request and the browser won't do a page refresh.

The solution should be a two step process:

  1. Execute the server action to save the data in session as an ajax request.

  2. After the ajax request has been processed, you should open your window using JavaScript.

The <f:ajax> component by nature doesn't offer an oncomplete javascript method,but you can use the power of the onevent method to execute a javascript code after completing the ajax request. The example is based on JSF 2.0 javascript onload/oncomplete.

<h:commandLink action="#{main.setClickedId(items.itemId)}" value="Hello">
    <f:ajax onevent="handleOnComplete" />
</h:commandLink>

<h:outputScript>
    function handleOnComplete(e) {
        if (e.status == 'success') {
            openWin2();
        }
    }
</h:outputScript>

Another way to do it would be using third party libraries like <a4j:commandLink> from RichFaces or <p:commandLink> from PrimeFaces. I'll post a sample using PrimeFaces code:

<p:commandLink action="#{main.setClickedId(items.itemId)}" value="Hello"
    oncomplete="openWin2()" />
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I tried the thing with without any success. It just redirects me to the same URL just with a # at the end. And I had to set a name for . Can I set a random name or has it to be named properly? – Samuel Brand Feb 02 '13 at 21:45
  • @SamuelBrand You can replace the `` for ` – Luiggi Mendoza Feb 02 '13 at 21:59
  • That works fine with opening the new window :) But it seems like the key isn't saved in the session – Samuel Brand Feb 03 '13 at 00:37
  • @SamuelBrand my mistake, it is `event="action"` on the ``, so there was no need to add the `event` attribute to begin with. I've updated my answer. – Luiggi Mendoza Feb 03 '13 at 01:02
  • Window is opening. Key isn't saved :( Maybe there is something wrong with my bean? – Samuel Brand Feb 03 '13 at 01:30
  • @SamuelBrand The problem might be that you're not successfully storing the data in session or you've recovering it the wrong way. If you add the definition of your beans and the methods to save and load the data to/from session then you could get better help. – Luiggi Mendoza Feb 03 '13 at 18:03
  • I add the method to load the data in the question. The method to save it, is already in – Samuel Brand Feb 04 '13 at 17:40
  • @SamuelBrand with the code provided the error cannot be reproduced. Please add an example in form of [SSCCE](http://sscce.org) to analyze the problem in depth. – Luiggi Mendoza Feb 04 '13 at 18:35
  • I tried to do this. Here is the link to the [ProjectFile](http://www.file-upload.net/download-7164366/SessionProblem.zip.html) Thanks for your patience and for your help! :) – Samuel Brand Feb 05 '13 at 13:51
  • @SamuelBrand check if the `` is inside a ``, debug if the action method to save the data in session gets executed and if the load method gets executed too. – Luiggi Mendoza Feb 05 '13 at 14:16
  • Wow! It seems like only the saving method is beeing called! – Samuel Brand Feb 05 '13 at 15:28
  • @SamuelBrand please provide the complete bean code that you use to load the data. – Luiggi Mendoza Feb 05 '13 at 17:08
0

I have spotted the fail by myself.
The problem was, that the JSF Page did not parse the JSF Tags.
From:

<h:outputText value="#{items.itemId" />

I got nothing as output.
That's why I thought that the JSF page failed on calling the methods.
But this was not the problem. The problem was my URL. That's my code to open the second page:

openWin2('immobilie.xhtml')

But thats wrong. It has to be

openWin2('faces/immobilie.xhtml')

I got the solution from here.


Luiggi Mendoza thank you so much for helping me! You helped me very much!


Community
  • 1
  • 1
  • You could have solved the problem long before if your servlet-mapping for the Faces Servlet was `*.xhtml` instead of `/faces/`. Also, this is the solution for a problem not stated in the question. Anyway, you're welcome. – Luiggi Mendoza Feb 05 '13 at 21:28