Friday, May 9, 2008

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

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)

%>

Read More...

Wednesday, May 7, 2008

Read custom created XML file from a Groovy/Java class in a J2EE Web Application

I have a following xml file that I created to store the application name and Application Owner email address

//File name: EmailInfo.xml

< emailInfo>
< application> < name> Saleways < /name> < email> david@david.com< /email> < /application>
< application> < name> Mercantile House< /name> < email> binod@hotmail.com< /email> < /application>
< application> < name> BishowShop< /name> < email> bishow@hotmail.com< /email> < /application>
< /emailInfo>


Now I have a J2EE application that contains a Groovy or a Java class that access the above XML file. The method to access above html file using Groovy is presented below. Note that the xml file resides on the same location where the class exists.


def appName=[]
def appEmail=[]
java.net.URL url= this.getClass().getClassLoader().getResource("EmailInfo.xml");
java.net.URI uri = new URI(url.toString())
File f=new File(uri)
def emailInfo = new XmlSlurper().parse(f) //Specific to groovy
emailInfo.application.name.each {appName += it} //specific to groovy
emailInfo.application.email.each {appEmail += it} // specific to groovy

You simply can modify the above code for java classes. The final result for the above code is that appName contains the list of the Name and appEmail contains the list of the Email address. You can put it into hash map as below.

def emailMap=[:] //define an empty map
for(i in 0 .. appName.size()-1)
emailMap.put(appName[i].toString(),appEmail[i].toString())

Now you simply can access the email address of a person as shown below

println emailMap[“Mercantile House”] //prints binod@hotmail.com

Read More...

Welcome to my Technical Blog!

I create this blog so that i can share all the technical stuffs that i came across as a software engineer in J2EE Technologies. I will be glad to receive your feedback for every post and share your idea on the postings.
Welcome you all.
~Bishow
Read More...

Pages

 ©mytechtoday.com 2006-2010

 ©Mytechtoday

TOP