I am using jsf. When I used the h:commandButton in the jspx page. I could call the function of the managed bean to return a string then post to other page. I would like to ask could I call the managed bean to do the action when I first entered the page. I would like to fire the action in the managed bean without clicking any button. Is it possible?
Asked
Active
Viewed 51 times
1 Answers
0
You can do this using PreRenderView event. Lets say you have a managedBean like this
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
@ManagedBean(name="listener")
@SessionScoped
public class ListenerBean{
public void handleEvent(ComponentSystemEvent event){
// Handle event, do your thing, and redirect again if you need
FacesContext fc = FacesContext.getCurrentInstance();
ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc.getApplication().getNavigationHandler();
nav.performNavigation("my-navigation-rule");
}
}
And on your xhtml (sorry I do not know jspx version of it, I hope you can convert it to your templating style).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:event listener="#{listener.handleEvent}" type="preRenderView" />
<h:body>
....
</h:body>
</html>

bhdrkn
- 6,244
- 5
- 35
- 42