jsf with java injection
Java Injection (JSR-299) gives JSF a solid foundation for
its component model, based on Java Injection's typesafe IoC capabilities
and annotation-based discovery. Demo
Java Injection works together with JSF to provide a solid
component configuration for the data model of a JSF application.
With Java Injection, component classes are automatically registered
through classpath scanning,, reducing the amount of configuration XML to
a minimum. In this example, we only need XML to define the FacesServlet,
and a marker beans.xml to direct Java Injection 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.
The data model is the heart of the JSF application. In this case,
a trivial calculator.
The example.Calculator is a Java Injection Simple Bean. When Resin scans the classes,
it will discover Calculator , introspect it, and automatically
register the calculator. Once it's registered, any other
Java Injection 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.
Calculator.java
package example;
import javax.context.RequestScoped;
import javax.annotation.Named;
@RequestScoped
@Named("calc")
public class Calculator {
private int _a;
private int _b;
public int getA() { return _a; }
public void setA(int a) { _a = a; }
public int getB() { return _b; }
public void setB(int b) { _b = b; }
public int getSum()
{
return _a + _b;
}
}
The @RequestScoped annotation tells Java Injection 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 a name
for the calculator so JSP EL expressions and JSF can access it.
If there is no @Named , the bean cannot be used in a JSP
EL expression.
Java Injection components can also be injected with other
Java Injection, 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
Calculator getSum() method.
- <h:commandButton> creates a HTML <input type="submit"> tag.
test.jsp
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<f:view>
<h:messages/>
<h:form>
<h:inputText value="#{calc.a}" size="4"/>
+ <h:inputText value="#{calc.b}" size="4"/>
= <h:outputText value="#{calc.sum}" style="color:red"/>
<br>
<h:commandButton value="Add"/>
</h:form>
</f:view>
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 beans.xml
The housekeeping overhead is a minimum when using Java Injection.
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 beans.xml
Java Injection will scan classes directories and jars if they contain
a META-INF/beans.xml file, so many applications will
just use beans.xml as a marker file with no content.
Others applications will want to configure the Java Injection components using
the beans.xml or may put that configuration in the
resin-web.xml .
WEB-INF/resin-web.xml
<web-app xmlns="http://caucho.com/ns/resin">
<servlet-mapping url-pattern="*.jsf"
servlet-class="javax.faces.webapp.FacesServlet"/>
</web-app>
META-INF/beans.xml
<Beans xmlns="urn:java:ee">
<!--
- The beans.xml marks a class root for Java Inject to search for
- simple beans. Since the example doesn't need to override any
- defaults, there's no additional configuration necessary.
-->
</Beans>
Completing the Application
A more complete application would likely the IoC injection capabilities
of Java Injection. For example:
- Use Java Persistence by injecting a
@Current EntityManager
to a model field.
- Injecting a Java injection singleton service
with
@Current , defined
by a <foo:MyBean> configuration in the resin.xm. (assuming it needs configuration.
- Using JDBC directly with
@Current DataSource or @Name("jdbc/test") DataSource. .
- Using EJB stateless or stateful session beans as services.
Demo
Copyright © 1998-2011 Caucho Technology, Inc. All rights reserved. Resin ® is a registered trademark, and Quercustm, Ambertm, and Hessiantm are trademarks of Caucho Technology. |
|