Friday, May 9, 2008

How to read a Java Session Back bean of a JSF page from JSP pages

Facebook

In this section I am describing about accessing a JSF back bean from a JSP page.
Lets us take an example of a java back bean ServerInfo for a JSF page

// File Name : ServerInfo.java
package thomson.com;
public class ServerInfo {
String server;
String environment;

public String getServer() {
return server;
}

public void setServer(String server) {
this.server = server;
}
}

Bean is defined on faces-config.xml as following

<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<to-view-id>/process.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>ServerInfoBean</managed-bean-name>
<managed-bean-class>thomson.com.ServerInfo</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Now lets take an example of JSF page section that sets up the java bean ServerInfoBean from the input text of the form as shown below

//File name: index.jsp
<f:view>
<h:form id="serverForm">
<h:outputLabel for="serverName">
<h:outputText value="Enter the Server Name"/>
</h:outputLabel>
<h:inputText id="serverName" value="#{ServerInfoBean.server}" required="true"/>
<h:message for="serverName"/>
<br>
<h:outputLabel for="environment">
<h:outputText value="Select Environment"/>
</h:outputLabel>
</h:form>
</f:view>

Now in process.jsp the back bean can be accessed as shown below

File name : process.jsp
..
<%
ServerInfo mbean = (ServerInfo) request.getSession().getAttribute("ServerInfoBean");
String server = mbean.getServer();
Out.print(“server name read from the jsf back bean =” +server)

%>

0 comments:

Post a Comment

I love to entertain onymous user Comment !

Pages

 ©mytechtoday.com 2006-2010

 ©Mytechtoday.com, Learning from Experiences !

TOP