jsf portlet backing beans and ui components copyright © 2000-2007 liferay, inc. all rights...

23
JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission from Liferay, Inc.

Upload: adrian-townsend

Post on 26-Mar-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

JSF PortletBacking Beans and UI Components

Copyright © 2000-2007 Liferay, Inc.

All Rights Reserved.No material may be reproduced electronically or in print without written

permission from Liferay, Inc.

Page 2: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Objective

1. Add JSF UI Components– index.jsp

2. Register & create the Backing Bean with JSF– faces-config.xml

3. Bind Backing Bean Property to UI Component– index.jsp

Page 3: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Key Concepts

UI Component– A Stateful object, maintained on the server, that

provides functionality for interacting with an end user.– UI components are JavaBeans with properties,

methods, and events.– Organized into a view, which is a tree of components

usually displayed as a page.

Backing Bean– Specialized JavaBeans that collect values from UI

components and implement event listener methods.

Page 4: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Add JSF UI Components

Modify …/ext/portlets/library_jsf.war/index.jsp and add some UI components.

Page 5: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

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><br/><h:outputText value="Book Title:" /><h:inputText id="bookTitle" /><br/><br/>

</h:form>

</f:view>

Page 6: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Deploy the Files to Tomcat

• Open up a cmd prompt

– Click “Start”, “Run” and then type “cmd”

• Navigate to your ext\portlets directory and then type “ant compile deploy”

• …\ext\portlets>ant compile deploy

• From your browser, Click Home A1 or use CTRL-F5 to reload the portlet

Page 7: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Register your Backing Bean with JSF

Create …/ext/portlets/library_jsf_portlet.war/WEB-INF/faces-config.xml.

This file is used to store all of your JSF configuration information:

Page 8: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

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"><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-bean></faces-config>

Page 9: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Create your Backing Bean

In …/ext/portlets/library_jsf_portlet.war/WEB-INF/src, use Eclipse to create com.ext.portlet.library.ui.BookBean:

Page 10: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

BookBean.javapackage com.ext.portlet.library.ui;

import java.util.ArrayList;import java.util.List;

import javax.faces.application.FacesMessage;import javax.faces.context.FacesContext;import javax.faces.event.ActionEvent;

public class BookBean {public String getTitle() {

return _title;}

public void setTitle(String title) {_title = title;

}

private String _title;}

Page 11: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Initialize Values

Modify faces-config.xml.

This will initialize BookBean.title every time it is created (per request):

Page 12: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

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"><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>

</faces-config>

Page 13: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Bind Backing Bean Property to UI Component

Modify index.jsp.

Adding value=“#{book.title}” binds the title property of the book bean to this input field.

Page 14: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

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><br/><h:outputText value="Book Title:" /><h:inputText id="bookTitle" value="#{book.title}"/><br/><br/>

</h:form>

</f:view>

Page 15: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Deploy the Files to Tomcat

• Open up a cmd prompt

– Click “Start”, “Run” and then type “cmd”

• Navigate to your ext\portlets directory and then type “ant compile deploy”

• …\ext\portlets>ant compile deploy

• From your browser, Click Home A1 or use CTRL-F5 to reload the portlet

Page 16: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Create Command Button

Modify index.jsp and add a command button.

We will be using this button in a later exercise to add books to the database:

Page 17: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

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><br/><h:outputText value="Book Title:" /><h:inputText id="bookTitle" value="#{book.title}"/><br/><br/>

<h:commandButton value="Add Book" /></h:form>

</f:view>

Page 18: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Register Simple Action Listener

Modify index.jsp.

Bind the BookBean.addBook() to the command button’s actionListener.

When the form is submitted JSF will generate an action event that will invoke this actionListener:

Page 19: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

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><br/><h:outputText value="Book Title:" /><h:inputText id="bookTitle" value="#{book.title}"/><br/><br/>

<h:commandButton value="Add Book" actionListener="#{book.addBook}" /></h:form>

</f:view>

Page 20: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Handle Action Event

Modify BookBean.java.

Add the addBook() to handle the action event generated by the commandButton. The event handler will clear the title field when it is invoked:

Page 21: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

BookBean.javapackage com.ext.portlet.library.ui;

import java.util.ArrayList;import java.util.List;

import javax.faces.application.FacesMessage;import javax.faces.context.FacesContext;import javax.faces.event.ActionEvent;

public class BookBean {public String getTitle() {

return _title;}

public void setTitle(String title) {_title = title;

}

public void addBook(ActionEvent actionEvent) {// clear the titlesetTitle("");

}

private String _title;}

Page 22: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Deploy the Files to Tomcat

• Compile and redeploy, restart Tomcat and refresh browser.

• Verify that the input field is being cleared after you submit the form.

Page 23: JSF Portlet Backing Beans and UI Components Copyright © 2000-2007 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in

Revision HistoryJames Min 01/17/2007

Ivan Cheung 01/30/2007