File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Portals and Portlets and the fly likes My JSF Portlet Tutorial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Portals and Portlets
Bookmark "My JSF Portlet Tutorial" Watch "My JSF Portlet Tutorial" New topic
Author

My JSF Portlet Tutorial

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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.
<hutputText 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.

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Oct 11, 2004
Posts: 3830
Thanks Cameron!
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
A readable version can be found here: http://ankurrathi.googlepages.com/jsfportlets
ganesh boil
Greenhorn

Joined: Sep 17, 2009
Posts: 14
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!!
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Thanks for the tutorial..
its could be used as my first step to begin develop application with Portlet..

Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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.


Samik Mukherjee
Greenhorn

Joined: Oct 29, 2002
Posts: 3
Ankur,

Nice to start with. But where did you get the portlet class along with its doView() and processAction() methods in a JSF portlet?
 
Don't get me started about those stupid light bulbs.
 
subject: My JSF Portlet Tutorial
 
Similar Threads
navigation rules are not working...
commandLink param problem
inputText tag not working properly
Help. commandButton does not invoke Bean's method.
FacesContext is null inside managed bean