Developing the View Class

Before we look at the custom ViewHandler, let’s look at a class that creates a view. The com.mycompany.newsservice.views.SubscribeView class creates a view that’s identical to the newsletter subscription example in Chapter 2:

package com.mycompany.newsservice.views; import javax.faces.application.Application; import javax.faces.component.UICommand; import javax.faces.component.UIForm; import javax.faces.component.UIInput; import javax.faces.component.UIOutput; import javax.faces.component.UIPanel; import javax.faces.component.UISelectItems; import javax.faces.component.UISelectMany; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; import javax.faces.model.SelectItem; import javax.faces.el.MethodBinding; import javax.faces.el.ValueBinding; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.mycompany.jsf.pl.View; public class SubscribeView implements View { public UIViewRoot createView(FacesContext context) { Application application = context.getApplication( ); UIViewRoot viewRoot = new UIViewRoot( ); UIForm form = new UIForm( ); viewRoot.getChildren( ).add(form); UIPanel grid = new UIPanel( ); grid.setRendererType("javax.faces.Grid"); grid.getAttributes( ).put("columns", "2"); UIOutput emailLabel = new UIOutput( ); emailLabel.setValue("Email Address:"); grid.getChildren( ).add(emailLabel); UIInput email = new UIInput( ); ValueBinding emailAddr = application.createValueBinding("#{subscr.emailAddr}"); ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.