1

Currently I'm learning RichFaces and read through some demo on site,http://livedemo.exadel.com/richfaces-demo/richfaces/commandButton.jsf?c=commandButton&tab=usage

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:a4j="http://richfaces.org/a4j"
  xmlns:rich="http://richfaces.org/rich">

<style>
    .gridhello {
        border-collapse : collapse;
    }
    .gridhellocolumn {
        padding-left : 0;
    }
    .outhello {
        font-weight: bold;
    }
</style>

<a4j:form>
    <h:panelGrid columns="3" styleClass="gridhello" columnClasses="gridhellocolumn">
        <h:outputText value="Name:" />
        <h:inputText value="#{userBean.name}" />
        **<a4j:commandButton value="Say Hello" reRender="out" />**
    </h:panelGrid>
</a4j:form>
<rich:spacer height="7"/>
<br />
<h:panelGroup id="out">
    <h:outputText value="Hello " rendered="#{not empty userBean.name}" styleClass="outhello" />
    <h:outputText value="#{userBean.name}" styleClass="outhello" />
    <h:outputText value="!" rendered="#{not empty userBean.name}" styleClass="outhello" />
</h:panelGroup>

<br />

code line has no "action" attribute, I'm just wondering how it still works, what's the default method of back bean was called in this case so that userBean.name was updated?

Thanks in advance!

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user1540244
  • 11
  • 1
  • 2

1 Answers1

3

When you do a submit, the form data is sent into the mapped attributes of the managed bean. That's default behavior for UICommands: <h:commandButton>, <h:commandLink>, <a4j:commandButton>, <a4j:commandLink> and others like <a4j:jsFunction>. Even if you don't declare an action to execute, the form data is submitted to the server through a POST request and the JSF lifecycle will fire. As part of the lifecycle, in the Update Model phase, JSF will set the data bound from the components into the managed bean attributes, then the Invoke Applications phase will perform any action (if available) and in the end the Render Response phase will generate the response and update the View (JSP, Facelets). In this case, since it's an ajax call, the view will be updated; more exactly, the component with id "out" will be updated. Note that this "out" content is outside the <a4j:form>, the values here won't be sent to server, still the reRender will update their values with the view model values.

UPDATE:

The line <h:inputText value="#{userBean.name}" is binding the name attribute of the UserBean managed bean with the value of the <h:inputText> component. This is a basic concept from JSF. Remember that RichFaces is not a JSF implementation, it extends the framework and add functionalities like ajax support for RIA.

This line

<h:outputText value="#{userBean.name}" styleClass="outhello" />

means: bind the value of name attribute of the UserBean managed bean to the outputText component. Still, this is outside the form, so the actual value of this component won't be sent on the server.

Some resorces to learn more about JSF:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Hi Luiggi, thanks for your detailed explanations! After your comments, however, there is still one thing I'm not so clear about here. __ has no name attribute and is not bound to any field of back bean. How could RichFaces/JSF figure out such as connection between this inputText and a certain field of back bean when this form is submitted? It'll be great if you point to me some documentations on this issue. And I can read it further by myself. Thanks in advance! – user1540244 Jul 21 '12 at 08:42
  • 2
    @user1540244 answer edited. An advice: before you start learning RichFaces (or another JSF based framework like PrimeFaces or OmniFaces) I recommend you to have a good understanding of plain JSF. – Luiggi Mendoza Jul 21 '12 at 15:40