Transcript Slide 1

JSF Portlet
Navigation and Event Listeners
Copyright © 2000-2007 Liferay, Inc.
All Rights Reserved.
No material may be reproduced electronically or in print without written
permission from Liferay, Inc.
Objective
1. Create navigation rules
– faces-config.xml
2. Implement navigation logic
– index.jsp
– display_books.jsp
3. Register an event listener to implement the
navigation flow
– index.jsp
– BookBean.java
Create Navigation Rules
Modify index.jsp.
Modify faces-config.xml. Here we are adding
two navigation rules.
1) “On ‘success’ from /index.jsp, forward to
/display_books.jsp.”
2) “On ‘back’ from /display_books.jsp,
forward to /index.jsp.”
faces-config.xml
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config xmlns="http://java.sun.com/JSF/Configuration">
<factory/>
<managed-bean>
<managed-bean-name>book</managed-bean-name>
<managed-bean-class>com.ext.portlet.library.ui.BookBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>title</property-name>
<value>&lt;book name&gt;</value>
</managed-property>
</managed-bean>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/display_books.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/display_books.jsp</from-view-id>
<navigation-case>
<from-outcome>back</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
Implement Navigation Logic
1) Modify index.jsp.
2) Create display_books.jsp.
index.jsp
Modify index.jsp.
This will generate the “success” outcome
when the Display Books command button
is invoked. immediate=”true” tells JSF to
skip validation on this button, since we
don’t care about the input value when we
submit the form.
index.jsp
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<h1>
<h:outputText value="Simple JSF Portlet" />
</h1>
<h3>
<h:outputText value="Add a book entry to the library:" />
</h3>
<h:form>
<h:messages />
<br/>
<h:outputText value="Book Title:" />
<h:inputText id="bookTitle" value="#{book.title}">
<f:validateLength minimum="2" maximum="30" />
</h:inputText>
<br/>
<br/>
<h:commandButton value="Add Book" actionListener="#{book.addBook}" />
<h:commandButton action="success“ value="Display Books" immediate="true" />
</h:form>
</f:view>
display_books.jsp
Create
…/ext/portlets/library_jsf_portlet.war/displa
y_books.jsp.
display_books.jsp
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<h1>
Display Books
</h1>
<h:form>
<h:commandButton action="back" value="Back"/>
</h:form>
</f:view>
display_books.jsp
<h:commandButton action="back"
value="Back"/>
This will generate the “back” outcome when
the Back command button is invoked.
Deploy the Files to Tomcat
• Redeploy
• Refresh
• Verify
Register an event listener to
implement the navigation flow
• Modify index.jsp.
• Modify BookBean.java and create
BookBean.displayBooks().
index.jsp
Modify index.jsp.
Instead of hard-coding the outcome we
register an event listener to generate the
outcome.
index.jsp
<f:view>
<h1>
<h:outputText value="Simple JSF Portlet" />
</h1>
<h3>
<h:outputText value="Add a book entry to the library:" />
</h3>
<h:form>
<h:messages />
<br/>
<h:outputText value="Book Title:" />
<h:inputText id="bookTitle" value="#{book.title}">
<f:validateLength minimum="2" maximum="30" />
</h:inputText>
<br/>
<br/>
<h:commandButton value="Add Book" actionListener="#{book.addBook}" />
<h:commandButton action="#{book.displayBooks}" value="Display Books" immediate="true" />
</h:form>
</f:view>
BookBean.java
public class BookBean {
public String getTitle() {
return _title;
}
public void setTitle(String title) {
_title = title;
}
public void addBook(ActionEvent actionEvent) {
// clear the title
FacesContext facesContext = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage(getTitle() + " was added successfully.");
// null signifies that this message is not intended for any ui component
facesContext.addMessage(null, message);
// clear the title
setTitle("");
}
public String displayBooks() {
return "success";
}
private String _title;
Deploy the Files to Tomcat
•
•
•
•
Compile
Redeploy
Refresh
Verify
Revision History
James Min
Ivan Cheung
01/17/2007
01/30/2007