4

I am new to JSF (2).

In Struts we can invoke an action from a URL like app.action. This invokes an action and returns a result page, say a JSP (initially there is no JSP/HTML).

How can the same thing be done in JSF? (I know how to invoke an action from an .xhtml) I.e. invoking a managed bean directly from a URL and getting a result page.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
AnilHoney
  • 259
  • 8
  • 20

2 Answers2

5

If it's preparing data for an initial GET request, just do the job in (post)constructor of the request or view scoped managed bean associated with the page.

@ManagedBean
@RequestScoped
public class Bean {

    public Bean() {
        // Here.
    }

    @PostConstruct
    public void init(){ 
        // Or here, certainly if you rely on injected dependencies like @EJB.
    }

}

If it's controlling the request/response and may possibly redirect/navigate to another page, then do the job in preRenderView.

<f:event type="preRenderView" listener="#{bean.listener}" />

with

public void listener() {
    // ...

    // You want to redirect?
    externalContext.redirect(newURL);

    // Or you want to navigate?
    navigationHandler.handleNavigation(context, null, "newOutcome");
}

Or if you want to hook on all requests, then use a filter:

@WebFilter("/*")
public class MyFilter implements Filter {

    // ...

}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Instead of a `@Webfilter`, I think AnilHoney might be searching for a `@WebServlet` to handle a specific pattern? – Menno Nov 21 '12 at 16:30
  • @Aquillo: filters are for modifying requests, servlets are for controlling requests to views. OP's case (there's no "view" at all) sounds too much like that he want to modify requests. – BalusC Nov 21 '12 at 16:31
  • 1
    As I read the question, he wants to execute some logic, before landing on the desired view page. Nevertheless I would recommend your first given option, as long as the corresponding logic can be executed upon loading the page, instead of before loading the page. – Menno Nov 21 '12 at 16:36
  • @Aquillo: as far as I understand it, the desired view page is only known *after* executing the logic. A servlet is then definitely insuitable. – BalusC Nov 21 '12 at 16:37
  • Oh I am sorry, I didn't understand that OP would be using this logic to determine which page he needs loaded. In that case you're right to define a filter. – Menno Nov 21 '12 at 16:50
  • yes guys sorry for the late ,very thanks for ur support a simple example is i want to land my website homepage as a result of [link]:http://localhost:8080/myapp/homepage.jsf that must not use any index.jsp to redirect or forwading concept like must not be JSp:forward(must be done with out using any jsps) – AnilHoney Nov 22 '12 at 05:38
5

The pattern you're asking about is not really native to the way JSF works.

Request-based frameworks like Struts and Spring MVC, and the older Model-2 Servlet/JSP based approach did work like this.

In JSF, first and foremost it's the page (view) that's automatically mapped to a request URL. There's no notion of a bean that's directly mapped to a URL and also there's no notion of a bean that has a framework enforced 1:1 relation with a view. There is the concept of a backing bean though, but this is by convention. For JSF, all beans are "helper beans" that are merely referenced by the view.

BalusC outlined the popular ways that can be used today to get some of the behavior from request-based frameworks in JSF in his answer. JSF 2.2 will slightly expand on this support via the introduction of view actions, which formalizes a few of the typical use cases for the preRenderViewEvent.

That said, JSF is a very flexible framework and very few things are set in stone. A lot of JSF's behavior can be replaced or added to via an elaborate plug-in and decorator system.

For this use case, the fact that JSF binds URLs to (Facelets) views can be overridden, and you can in fact let beans directly react to requests. Although for a slightly different purpose, this is what I essentially did for JavaVDL, by overriding the so-called view handler.

You do have to ask yourself if this is really what you want to do, and if you want to work in this way whether JSF is the best choice for you. But via the methods outlined in JavaVDL (sourcecode here), you should be able to do this. Do note that this last method is not suited for beginners and requires quite a lot of JSF experience if you want to pull this off yourself. (If you, or anyone else would like to have this functionality, please consider creating an issue for it at the OmniFaces issues list).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140