JSF 2: calling a managed bean directly

I am new to JSF (2).

In Struts, we can invoke an action with a URL, such as app.action . This invokes the action and returns a results page such as JSP (initially no JSP / HTML).

How can I do the same in JSF? (I know how to invoke an action from .xhtml) That is, by invoking a managed bean directly from the URL and getting a results page.

+4
source share
2 answers

The sample you are asking for is not really native to how JSF works.

Query-based frameworks such as Struts and Spring MVC , and the old Servlet / JSP approach for Model 2, actually worked like this.

In JSF, first of all, it is a page (view) that is automatically mapped to the request URL. There is no concept of a bean that maps directly to a URL, and no concept of a bean that has a structure associated with a 1: 1 relationship with a view. However, there is a concept of bean support, but this is consistent. For JSF, all beans are "auxiliary beans" that are simply referenced by the view.

BalusC outlined popular ways that can be used today to get some of the behavior from query-based frameworks in JSF in his answer. JSF 2.2 will expand this support a bit by introducing a view action that formalizes a few typical use cases for preRenderViewEvent .

However, JSF is a very flexible structure, and very few things are set in stone. Many JSF actions can be replaced or added through a complex system of plugins and decorators.

For this use case, the fact that JSF associates URLs with Facelets can be overridden, and you can really let beans respond directly to requests. Although for a slightly different purpose, I did this for JavaVDL , overriding the so-called view handler.

You should ask yourself if this is really what you want to do, and if you want to work that way, is JSF the best choice for you. But using the methods described in JavaVDL ( sourcecode here ), you have to do this. Please note that this last method is not suitable for beginners and requires quite a lot of JSF experience if you want to take it off yourself. (If you or anyone else would like to have this functionality, consider creating a problem for it in the OmniFaces problem list ).

+5
source

If it is preparing the data for the initial GET request, just do the job in the (post) request constructor or view the 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 controls the request / response and can redirect / go to another page, complete the task 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 include all requests, use filter :

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

See also:

+5
source

Source: https://habr.com/ru/post/1447423/


All Articles