Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect to an anchor in JSF?

Let's say I have this action in a JSF Managed Bean:

public String doSomething() {
    FacesContext.getCurrentInstance().getExternalContext().getFlash().put("msg", "Something was done successfully");
    return "view?faces-redirect=true";
}

My view has an anchor element with the id msg. I want the url to have this anchor (for accessibility matters), like:

view.jsf#msg

Or whatever is my FacesServlet filter pattern.

return "view#msg?faces-redirect=true"; obviously will not work because JSF (mojarra at least) will try to evaluate view#msg as a view.

So my question is how to make JSF redirect to a URL with #msg in the end.

like image 778
bluefoot Avatar asked Jul 06 '11 19:07

bluefoot


1 Answers

because JSF (mojarra at least) will try to evaluate view#msg as a view

Oh, that's nasty. It's definitely worth an enhancement request at the JSF/Mojarra boys.

Your best bet is to send the redirect manually with help of ExternalContext#redirect().

public void doSomething() throws IOException {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.getFlash().put("msg", "Something was done successfully");
    ec.redirect("view.xhtml#msg");
}

(assuming that FacesServlet is mapped on *.xhtml)

Alternatively, you could conditionally render a piece of JS which does that instead.

<ui:fragment rendered="#{not empty flash.msg}">
    <script>window.location.hash = 'msg';</script>
</ui:fragment>
like image 94
BalusC Avatar answered Oct 16 '22 12:10

BalusC