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.
Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]