Saturday, October 11, 2008

Hello world Richfaces/ajax4jsf application

ajax4jsf/RichFaces

- is an open source framework that adds Ajax capability into existing JSF applications without resorting to JavaScript.


Step 1: (add jar files & make changes into the web.xml file)

...
    <context-param>
<param-name>org.richfaces.SKINparam-name>
        <param-value>blueSkyparam-value>
context-param>
<filter>
<display-name>RichFaces Filterdisplay-name>
<filter-name>richfacesfilter-name>
<filter-class>org.ajax4jsf.Filterfilter-class>

    <filter-mapping>
<filter-name>richfacesfilter-name>
<servlet-name>Faces Servletservlet-name>
<dispatcher>REQUESTdispatcher>
<dispatcher>FORWARDdispatcher>
<dispatcher>INCLUDEdispatcher>
>

Step 2:

class Bean {

private int first;

private int second;

private int result;

public String action() {

result = first+second;

return null;
}

}


Step 3:

index.jsp

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>

<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>

<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>

<h:form>

<rich:panel header="Calculate Sum" style="width:200px" > <!--allows to place the page elements in rectangle
panel that can be skinned
-->
First: <h:inputText
value="#{Bean.first}" /> <br>

Second: <h:inputText
value="#{Bean.second}" /> <br>

<%--reRender contains
the element id(or multiple ids) which will be updated after
the ajax response --%> <a4j:commandButton value="Add" action="#{Bean.action}"
reRender="result" />

Result: <h:inputText

value="#{Bean.result}" id="result" /> </rich:panel>
</h:form>


Source Code

No comments: