• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

My JSF Portlet Tutorial

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I wrote a small tutorial (article) on JSF Portlet. All kind of feedback is welcome.


Explaining Faces Portlet:

Version: 1.0
Author: Ankur Rathi
Related to: Steps to create Faces Portlet in RSA 7.0.doc

I will start with the life cycle of portlet � not JSF portlet.

There are two important methods in portlet class:
1.doView()
2.processAction()

When first time the page is requested, doView() method of the portlet gets invoked which is on that page, where you usually �include� a JSP page to be displayed. When user perform some form submit (or any kind of action), processAction() method gets called, followed by doView() method.

Now let�s see, where exactly we specify the portlet class. It�s in the <portlet-class> tag of <portlet> tag of portlet.xml file.

Let�s take a look at the <portlet-class> tag of our Faces Portlet:

<portlet-class>com.ibm.faces.portlet.FacesPortlet</portlet-class>

It will always be the same and we are not allowed to modify the code of FacesPortlet class.

Now the question is, how would you override doView() method of it? How would you �include� your JSP page?

Well, all you have to do is specify that JSP page in portlet.xml file at specific location:

<init-param>
<name>com.ibm.faces.portlet.page.view</name>
<value>/APortletView.jsp</value>
</init-param>

doView() method of FacesPortlet class is written in such a way that it picks up this JSP page and �include� it. It�s my guess only.

Now what about processAction() method? This is called when user performs some action (form submission, usually) on the page. Where to write logic to be executed on that action?

It�s little tricky. Look at the action attribute of submit button in JSP file:

<h:commandButton id="submit" action="#{LoginBean.login}" value="Login" />

It says that find a �managed bean� class with name LoginBean and execute login() method of it. What �managed bean� is, I will come to it in a moment.

And this finding operation won�t be performed in portlet.xml file. It�s in faces-config.xml file.

Let�s take a look at the <managed-bean> tag of faces-config.xml:

<managed-bean>
<managed-bean-name>LoginBean</managed-bean-name>
<managed-bean-class>com.infosys.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

It specifies the name for mapping, class name and scope of it.

So far so good. I write login() method in LoginBean class and I am done.

But what to do after executing business logic? Where to go? This, we will specify in <navigation-rule> tag of faces-config.xml:

<navigation-rule>
<from-view-id>/APortletView.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/greeting.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/APortletView.jsp</to-view-id>
</navigation-case>
</navigation-rule>

It looks pretty self explanatory. If login() method returns �success� go to gretting.jsp, if it returns �failure�, go to the same JSP - from where the action was performed - that is APortletView.jsp.

We come back to �managed bean� class. It�s a class which holds form parameters as well as has a method to perform business logic unlike struts where you need a separate �action class� to perform logic.

Now let�s talk about JSP file.

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

It starts with these two taglib directive that are required as we will be using tags of them throughout the JSP file.

I am explaining some of the most basic tags:

<f:view> - starts the page.
<h:form id="loginForm"> - starts the HTML form.
<h:outputText value="Username : " /> - kind of HTML label.
<h:inputText id="username" value="#{LoginBean.username}" required="true"> - HTML text box.
<h:commandButton id="submit" action="#{LoginBean.login}" value="Login" /> - HTML button.

Pay attention to the value attribute of <h:inputText> tag. It tells username property (or variable) of LoginBean class will hold the value of it.

That brings us to the end of this most basic tutorial on Faces Portlet.

 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
com.ibm.faces.portlet.FacesPortlet???

Great tutorial. You'd really be surprised how many people you'll help out.

For other portal servers other than IBM, you'll need to switch to a different portlet instead of the ibm portlet. That's what the apache bridges project is all about. People using JBoss or Pluto or JetSpeed can find the appropriate portal bridge here:

Apache Portlet Bridges Project

In a similar vain, I have a struts portlet tutorial for anyone interested:

Struts Portlet Tutorial

and a multimedia JSF tutorial to supplement as well:

Another Look at the JSF Portlet Bridge

Great work!

-Cameron McKenzie
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cameron!
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A readable version can be found here: http://ankurrathi.googlepages.com/jsfportlets
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankur,
This is a nice tutorial.
We are using Liferay Portal and try to use some jsf portlets.
To develope portlets we completely use SUN portlet API.

So here I have two questions.

1. Which portlet class to be used for liferay jsf portlets (These portlets are using sun portlet API).
2. Which portlet class to be used for liferay jsf portlets (If we use liferay Portlet API instead SUN api).

Thanks in advance!!
 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tutorial..
its could be used as my first step to begin develop application with Portlet..
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys & sorry for replying so late.

Ganesh, sorry, I haven't used either Liferay or Sun API.
Just a guess, it shouldn't be different than FacesPortlet.


 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankur,

Nice to start with. But where did you get the portlet class along with its doView() and processAction() methods in a JSF portlet?
 
reply
    Bookmark Topic Watch Topic
  • New Topic