Build url programmatically and redirect from ajax action

Splash Forums PrettyFaces Users Build url programmatically and redirect from ajax action

This topic contains 3 replies, has 2 voices, and was last updated by  detestable 3 years, 5 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #19661

    detestable
    Participant

    Hello,

    I’d like to redirect the user after an ajax request, I could do something like :

    FacesContext ctx = FacesContext.getCurrentInstance();

    ExternalContext extContext = ctx.getExternalContext();

    extContext.redirect("URL HERE");

    But I’d like to use pretty faces mapping to resolve the url from a string like :

    “/WEB-INF/index.xhtml?parameter=test” (that would resolve to /ctx/index/test)

    may be by building the url programmatically.

    How could one achieve this?

    #23467

    You could simply return the following string from the action method invoked by the AJAX request:

    /WEB-INF/index.xhtml?parameter=test&faces-redirect=true

    PrettyFaces will intercept that and rewrite the URL to the pretty one. :)

    #23468

    You could also use ExternalContext.encodeActionURL() to get the pretty URL:

    String url = externalContext.encodeActionURL("/WEB-INF/index.xhtml?parameter=test");
    assertEquals("/ctx/index/test", url);

    #23469

    detestable
    Participant

    Thank you very much Christian for your assistance,

    Using encodeActionURL in combination with FacesContext.getCurrentInstance().getExternalContext().redirect works perfectly fine.

    Lesson learned!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

Comments are closed.