Saturday, March 10, 2012

How JSF portlet can access EJB 3,0

This post will demonstrate how to call a local ejb session bean from a portlet.

In your Rad Create a new EJB project
select File->new ->project ->other and select EJB project.





         

and  Name the project as HelloWorld  and EAR project name as HelloWorldEAR as show below



                           click finish
this will create 3 projects in RAD (HelloWorld, HelloWorldClient and HelloWorldEar)  as shown below

Expand the HelloWorldClient and
right click on ejbModule
and select new-> Interface

                       and enter the name of the package and Interface names as shown in the figure above and click finish.

now open the HelloWorldLocal interface and add the lines in bold

@Local
public interface HelloWorldLocal {
    public String sayHello(String name);
}



@Local annotation makes the otherwise normal interface a Local interface
add @Remote if your ejb is a remote interface.
press ctrl+shift+o  to organize the imports.

Right click on HelloWorld project and select New->Class
enter the details as shown in the figure below
                       and click finish.

add @stateless annotation to designate it as a stateless session bean.

now we have to make this class implement the remote interface.
add sto the HelloWorld class .  Right click in the editor select source-> override/inherit methods
implements HelloWorldLocal

 
 
Right click in the editor select source-> override/inherit methods.
Your HelloWorld class should look like the picture above.
return "Hello " + name; 
add this line of code to sayHello method  .
 
 Creating JSF Portlet
Create a new portlet project 
 select File->new -> Project ->other->PortletProject
Enter the details as shown below
  
 
 and click next
check the Generate a custom portlet Class and enter the package and portlet name
  
as shown the above picture. and click finish.
 
Now expand the HelloWorldPortlet project and open faces-config.xml
  
you can see a managed bean entry created as shown in the picture below.

right click on the HelloWorldPortlet project  and select  properties
 
Select J2ee module dependencies and add select HelloWorldClient.jar as dependency as shown in the picture.
 
Now Open the HelloWorldPortletView  Managed bean class and add the below code
  @EJB
 HelloWorldLocal hwlocal ; 
 
 public String getName() {
  
  return hwlocal.sayHello("venkat");
 }
 
 
 
if you want to access the ejb from a POJO rather than from a servlet or a Managed bean 
 
open your web.xml  in your portlet 
and add the below lines

<ejb-local-ref>
  <description />

  <ejb-ref-name>HelloWorldEJBRef</ejb-ref-name>
  <ejb-ref-type>Session</ejb-ref-type>
  <local-home />
  <local>
   com.venkat.helloWorld.HelloWorldLocal
  </local>
  <ejb-link>HelloWorld</ejb-link>
 </ejb-local-ref>
 
and in your pojo class add the below code. to do the lookup.
 
HelloWorldLocal hwlocal = null;
      
  try {
   InitialContext ic = new InitialContext();
   
   hwlocal = (HelloWorldLocal) ic.lookup("java:comp/env/HelloWorldEJBRef");
    
                  } catch (NamingException e) { 
                  e.printStackTrace();
                  }
  return  hwlocal ; 
 
 
Now open HelloWorldPortletView.jsp and add the below lines to the jsp. 

<link rel="stylesheet" type="text/css" title="Style"
 href='<%= renderResponse.encodeURL(renderRequest.getContextPath() + "/theme/stylesheet.css") %>'>
<f:view><hx:scriptCollector id="scriptCollector1">
  
  <h:outputText styleClass="outputText" id="text1" value="#{pc_HelloWorldPortletView.name}"></h:outputText>
  <p></p>
 </hx:scriptCollector>
</f:view>

now right click on HelloWorld.EAR and export it as ear file. 
how login to Was Administration console for portal 
navigate to Applications->websphere enterprise applications-> and click on Install and select the 
exported EAR file and click next  and select the Detailed radio button. and click next. 
keep the default option and click next . 
in the next page make sure both the ejb and the portlet are deployed to WebSphere portal server
and not server1 as show below. 
 

and click next keep the default values in the next page and click next keep clicking next until you 
et to the below screen and enter the context root as shown below
 
and click next and then finish. 
Save the configuration and start the application you just installed if its not already started. 

Since the portlet is deployed as a J2EE application we have to register the portlet with portal. 
click here on instructions to register the predeployed j2ee appicaiton as portlet .
 
Now login in to Portal and go to Administration -> Webmodules 
and you should see the HelloWorld portlet . if the status says stopped. start it.  
Add the portlet to a page and access the page to see the portlet on the page. . 
 
 

1 comment:

  1. This one.Greetings! Very helpful advice within this article! It is the little changes that produce the greatest changes.Many thanks for sharing!if you interested in webcam so pleas visit Thanks for Your article it gives us immense knowledge.This post is exceptionally written. I can imagine how much hard work you have put into this so, congratulations.I have a Profile about you generate as many random lowercase and uppercase English alphabet letters as you need.Please Visit Here letter generator




    ReplyDelete

comments