Download presentation
Presentation is loading. Please wait.
1
JSF Portlet Navigation and Event Listeners
Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission from Liferay, Inc.
2
Objective Create navigation rules faces-config.xml
Implement navigation logic index.jsp display_books.jsp Register an event listener to implement the navigation flow BookBean.java
3
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.”
4
faces-config.xml <?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" " <faces-config xmlns=" <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><book name></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> <from-view-id>/display_books.jsp</from-view-id> <from-outcome>back</from-outcome> <to-view-id>/index.jsp</to-view-id> </faces-config>
5
Implement Navigation Logic
1) Modify index.jsp. 2) Create display_books.jsp.
6
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.
7
index.jsp taglib uri=" prefix="f" %> taglib uri=" 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> <h:commandButton value="Add Book" actionListener="#{book.addBook}" /> <h:commandButton action="success“ value="Display Books" immediate="true" /> </h:form> </f:view>
8
display_books.jsp Create …/ext/portlets/library_jsf_portlet.war/display_books.jsp.
9
display_books.jsp taglib uri=" prefix="f" %> taglib uri=" prefix="h" %> <f:view> <h1> Display Books </h1> <h:form> <h:commandButton action="back" value="Back"/> </h:form> </f:view>
10
display_books.jsp <h:commandButton action="back" value="Back"/>
This will generate the “back” outcome when the Back command button is invoked.
11
Deploy the Files to Tomcat
Redeploy Refresh Verify
12
Register an event listener to implement the navigation flow
Modify index.jsp. Modify BookBean.java and create BookBean.displayBooks().
13
index.jsp Modify index.jsp.
Instead of hard-coding the outcome we register an event listener to generate the outcome.
14
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> <h:commandButton value="Add Book" actionListener="#{book.addBook}" /> <h:commandButton action="#{book.displayBooks}" value="Display Books" immediate="true" /> </h:form> </f:view>
15
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); setTitle(""); public String displayBooks() { return "success"; private String _title;
16
Deploy the Files to Tomcat
Compile Redeploy Refresh Verify
17
Revision History James Min 01/17/2007 Ivan Cheung 01/30/2007
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.