Application Action Method Call (JSF)

We need to call the action method when the first page of the application is called. For example, the first page of index.jsp , when we directly call this page, the action method is not called. To do this, we wrote another page where she uses a java script to click on the button and call the action method, which moves to index.jsp.

I believe that JSF should be the right way to achieve this. What bet does that do? I told the team that we can call the action method in the constructor when the page loads. Is it correct? What are the possible solutions?

+4
source share
2 answers

Just do the job in the @PostConstruct application method with a bean scope that is eager ly built, or that is at least connected to the page.

 @ManagedBean(eager=true) @ApplicationScoped public class Bean { @PostConstruct public void init() { // Here. } } 

Alternatively, if JSF (read: the FacesContext ) does not have a corresponding role in the actual job, you can also use ServletContextListener .

 @WebListener public class Config implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { // Do stuff during webapp startup. } public void contextDestroyed(ServletContextEvent event) { // Do stuff during webapp shutdown. } } 

If you are not already on Servlet 3.0, register it in web.xml as follows.

 <listener> <listener-class>com.example.Config</listener-class> </listener> 

See also:

+11
source

Using JSF 2.0

If you want to take some action when starting the application (even if it is not already connected), you can use SystemEventListener and subscribe to PostConstructApplicationEvent.

Listener example:

 package listeners; import javax.faces.application.Application; import javax.faces.event.AbortProcessingException; import javax.faces.event.ListenerFor; import javax.faces.event.PostConstructApplicationEvent; import javax.faces.event.SystemEvent; import javax.faces.event.SystemEventListener; public class MySystemListener implements SystemEventListener{ @Override public void processEvent(SystemEvent event) throws AbortProcessingException { System.out.println("started"); } @Override public boolean isListenerForSource(Object source) { return source instanceof Application; } } 

To sign, you must include this snippet in faces-config.xml

 <application> <system-event-listener> <system-event-listener-class> listeners.MySystemListener </system-event-listener-class> <system-event-class> javax.faces.event.PostConstructApplicationEvent </system-event-class> </system-event-listener> </application> 

And if you want to take action when a user logs into a specific page, you can use another system event and the f: event tag to receive a notification before the page displays.

For instance:

 ... <h:body> <f:event type="preRenderView" listener="#{bean.action}"/> <h:form> <!--components--> </h:form> </h:body> ... 

Here is more detailed information on using system events: http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/#system-events .

In JSF 1.2, one of the ways I can get notified is with PhaseListener and check the identifier of the current rendering view.

+2
source

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


All Articles