JSF 2: invoking managed bean directly

前端 未结 2 1980
无人及你
无人及你 2021-01-19 21:21

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 (initiall

2条回答
  •  天命终不由人
    2021-01-19 21:43

    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.

    
    

    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:

    • Is there any easy way to preprocess and redirect GET requests?
    • How to make a redirection in JSF

提交回复
热议问题