Different ways to redirect with JSF and Seam

This is just a quick reminder for myself how to implement programmatic redirects with JBoss Seam and JSF.

I will describe three different techniques:

  • Redirecting to a 404 Error Page from a JSF Backing Bean
  • Redirecting to an external Website
  • Redirecting to a JSF view in a Seam Application

Redirecting to a 404 Error Page from a JSF Backing Bean

1
2
final HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
 response.sendError(HttpServletResponse.SC_NOT_FOUND);

Redirecting to an external Website

1
2
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
((HttpServletRequest) ec.redirect("http://example.com/");

Redirecting to a JSF view in a Seam Application
Thanks to Jeremiah Orr from Stackoverflow for this tip! (see: Stackoverflow Thread)

1
2
3
4
5
6
7
8
9
10
11
12
13
 
@Name("yourBean")
public class YourBean {
 
  @In
  Redirect redirect;
 
  public void yourMethod() {
     redirect.setViewId("/someView.xhtml");
     redirect.setParameter("someParam", "someValue");
     redirect.execute();
  }
}

OR

1
FacesManager.instance().redirect("/someView.xhtml", paramMap, conversationPropagationEnabled, includePageParams);
Share this Diese Icons verlinken auf Bookmark Dienste bei denen Nutzer neue Inhalte finden und mit anderen teilen können.
  • MisterWong
  • del.icio.us
  • Google Bookmarks
  • Facebook
  • TwitThis
  • DZone
  • Digg
  • Print

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert