Backing Beans
Sometimes, it is convenient to design a bean that contains some or all component objects of a web form. Such a bean is called a backing bean for the web form.For example, we can turn the BackingBean into a backing bean by adding properties or updating properties for the component you need on the form like this as follow:
edit.jsp:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<h:form id="form1">
<h:panelGrid binding="#{backingBean.component}"/>
<h:commandButton value="submit" actionListener="#{backingBean.action}"></h:commandButton>
</h:form>
</f:view>
BackingBean.java:
package com.ibm.bean;
import java.util.Iterator;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIPanel;
import javax.faces.component.UIViewRoot;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
public class BackingBean {
private FacesContext context = FacesContext.getCurrentInstance();
private UIViewRoot root = context.getViewRoot();
private Application application = context.getApplication();
private UIComponent component;
private UIInput input;
public UIInput getInput() {
if(input == null){
input = new UIInput();
}
return input;
}
public void setInput(UIInput input) {
this.input = input;
}
public BackingBean() {
}
public UIComponent getComponent() {
if (component == null) {
component = new UIPanel();
}
System.out.println(component);
return component;
}
// initialization by block or by static block
{
try {
HtmlOutputText out = (HtmlOutputText) context.getApplication()
.createComponent(HtmlOutputText.COMPONENT_TYPE);
HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
System.out.println("request.getRealPath()="+request.getRealPath(null));
System.out.println("request.getRequestURI()="+request.getRequestURI());
System.out.println("request.getContextPath()="+request.getContextPath());
System.out.println("request.getServletPath()="+request.getServletPath());
System.out.println("getViewId()="+context.getViewRoot().getViewId());
System.out
.println("----------this is the out component--------------"
+ this.getComponent());
out.setId("id1");
out.setValue("buttonName:");
HtmlInputText in = (HtmlInputText) context.getApplication()
.createComponent(HtmlInputText.COMPONENT_TYPE);
in.setRendered(false);
in.setId("button");
in.setValueBinding("buttonName", application
.createValueBinding("#{tableData.buttonName}"));
this.getComponent().getChildren().add(out);
this.getComponent().getChildren().add(in);
} catch (Throwable t) {
System.out.println("java.lang.Throwable exception encountered..."
+ t.getMessage());
t.printStackTrace();
}
}
public void setComponent(UIComponent component) {
this.component = component;
}
public void action(ActionEvent event){
HtmlInputText in = (HtmlInputText)findComponentInRoot("button");
in.setValue("@@@@");
in.setRendered(true);
System.out.println("getClientId="+event.getComponent().getClientId(context));
}
/**
* <p>
* Return the {@link UIComponent} (if any) with the specified <code>id</code>,
* searching recursively starting at the specified <code>base</code>, and
* examining the base component itself, followed by examining all the base
* component's facets and children. Unlike findComponent method of
* {@link UIComponentBase}, which skips recursive scan each time it finds a
* {@link NamingContainer}, this method examines all components, regardless
* of their namespace (assuming IDs are unique).
*
* @param base Base {@link UIComponent} from which to search
* @param id Component identifier to be matched
*/
public static UIComponent findComponent(UIComponent base, String id) {
// Is the "base" component itself the match we are looking for?
if (id.equals(base.getId())) {
return base;
}
// Search through our facets and children
UIComponent kid = null;
UIComponent result = null;
Iterator kids = base.getFacetsAndChildren();
while (kids.hasNext() && (result == null)) {
kid = (UIComponent) kids.next();
if (id.equals(kid.getId())) {
result = kid;
break;
}
result = findComponent(kid, id);
if (result != null) {
break;
}
}
return result;
}
/**
* find component based on root component of page, it calls <code>findComponent</code>
* method to find component
*
* @param id
* id of component which should be found
* @return
* component which has the id
*/
public static UIComponent findComponentInRoot(String id) {
UIComponent ret = null;
FacesContext context = FacesContext.getCurrentInstance();
if (context != null) {
UIComponent root = context.getViewRoot();
ret = findComponent(root, id);
}
return ret;
}
}
When the component tree for the form is built, the getComponent method of the backing bean is called, but it returns null. As a result, an output component is constructed and installed into the backing bean with a call to setComponent.
Backing beans have their uses, but they can also be abused. You should not use the user interface components as a repository for business data. If you use backing beans for your forms, you should still use beans for business objects.
I believe that it will help you above materials and need your help to continue.....