Files in this tutorial

Calculator model WebBean, taking the input and calculating the result.

WEB-INF/resin-web.xml

Configures FacesServlet.

WEB-INF/classes/META-INF/web-beans.xml

WebBeans configuration file.

Overview

WebBeans works together with JSF to provide a solid component-basis
for the data model of a JSF application.

With WebBeans, data components can be created by marking them with
a @Component annotation, reducing the amount of configuration XML to
a minimum. In this example, we only need XML to define the FacesServlet,
and a marker web-beans.xml to direct WebBeans to search for
component classes.

The data components automatically populate the JSF EL (expression language),
so they are automatically available to the JSF application.

This example creates a simple calculator which adds two numbers together.
The Calculator model receives the user data and
produces the results. A trivial JSP page creates the JSF UI
component tree.

Model Component

The data model is the heart of the JSF application. In this case,
a trivial calculator.

The @Component marks example.Calculator as
a WebBeans component. When WebBeans scans the classes, it will discover
Calculator, introspect it, and automatically register the
calculator in the WebBeans directory. Once it's registered, any other
WebBeans component, or JSP/JSF EL, or PHP file or servlet or EJB can
use the component.

The Calculator component has no XML configuration at all,
since there's nothing to configure. For other applications, some of the
component beans will want configuration to set properties, which will
occur in something like the resin-web.xml file.

The @RequestScoped annotation tells WebBeans to store
the bean in the servlet request scope. Each request will use its own
instance of the calculator. If the scope was @SessionScoped,
the same Calculator would be used for the entire session.
If it was @ConversationScoped it would be used for the
JSF page.

The optional @Named annotation gives an alternate name
for the calculator. If there is no @Named, WebBeans will
use the class name, e.g. "calculator" in this case.

WebBeans components can also be injected with other WebBeans, or
DataSources, JPA EntityManager or EntityManagerFactory
or JMS Queues, and they can also use the @PostConstruct
and @PreDestroy lifecycle annotations. Method interception
and event listening are also possible.

JSF/JSP: Building the Component Tree

JSF is designed around a UI component tree model. The JSP code
builds the JSF component tree, hands it back to JSF, and then JSF will
display the component tree based on its current rendering configuration.

<f:view> is a wrapper tag around all the JSF component tree.

<h:messages> displays any error messages, like typing a string to
the number fields.

<h:form> creates a HTML <form>

<h:inputText> creates a HTML <input> tag, using the
Calculator methods getA() and setA()
to receive the form values.

<h:outputText> creates a HTML <span> tag, with the text
value generated by the CalculatorgetSum() method.

The JSF expression language expressions #{calc.a} and
#{calc.b} are used in two phases of JSF. When displaying,
JSF will lookup the Calculator with "calc", and call
its getA() method. When processing the form, JSF will
lookup the Calculator and call the setA()
method to assign the new value.

Housekeeping: the resin-web.xml and web-beans.xml

The housekeeping overhead is a minimum when using WebBeans. In this
example we just need two pieces of XML configuration:

Configuring the JSF servlet in the web.xml

Marking a classpath root with a web-beans.xml

WebBeans will scan classes directories and jars if they contain
a META-INF/web-beans.xml file, so many applications will
just use web-beans.xml as a marker file with no content.
Others applications will want to configure the WebBeans components using
the web-beans.xml or may put that configuration in the
resin-web.xml.

Cloud-optimized Resin Server is a Java EE certified Java Application Server, and Web Server, and Distributed Cache Server (Memcached).Leading companies worldwide with demand for reliability and high performance web applications including SalesForce.com, CNET, DZone and many more are powered by Resin.