Access Managed Bean methods
As we know JSF managed beans are mostly access only through the JSP pages are faces-config.xml.But, there is some schenarios where you will need information stored in the managed beans for the business logic in some other beans. JSF provides API to access those values where ever you want.
also read:
Note that this example uses JSF 1.1 technique for accessing the values. These APIs are deprecated in JSF 1.2 and there is new APIs available from JSF 1.2 verion. The following sample programs use the normal programming API tp get the managed bean stored in the session and trying to invoke methods and properties.
JSP File (index.jsp)
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <html> <body> <f:view> <h:form> <h:commandButton action="#{jsfBean.submit}" value="Submit"/> </h:form> </f:view> </body> </html>
JavaBean (JavaBeatJsfBean.java)
package javabeat.net.jsf; import java.util.List; import javax.faces.application.Application; import javax.faces.context.FacesContext; import javax.faces.el.ValueBinding; /** * source : www.javabeat.net */ public class JavaBeatJsfBean { private List listValues; public List getListValues() { return listValues; } public void setListValues(List listValues) { this.listValues = listValues; } public String submit(){ FacesContext facesContext = FacesContext.getCurrentInstance(); Application application = facesContext.getApplication(); ValueBinding binding = application.createValueBinding("#{userInfo}"); UserInfo userInfo = (UserInfo)binding.getValue(facesContext); System.out.println("Method Calling : "+userInfo.getValuesNoArg()); System.out.println("User Name : "+userInfo.getUserName()); return "success"; } }
UserInfo.java
package javabeat.net.jsf; /** * source : www.javabeat.net */ public class UserInfo { private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getValuesNoArg(){ return "NoArgs"; } }
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?> <faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"> <managed-bean> <managed-bean-name>jsfBean</managed-bean-name> <managed-bean-class>javabeat.net.jsf.JavaBeatJsfBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> <managed-bean> <managed-bean-name>userInfo</managed-bean-name> <managed-bean-class>javabeat.net.jsf.UserInfo</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>userName</property-name> <value>JavaBeat</value> </managed-property> </managed-bean> <navigation-rule> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/index.jsp</to-view-id> </navigation-case> </navigation-rule> </faces-config>