HTML and CSS Reference
In-Depth Information
In situations where you do need state saving you should enable state saving on the server side rather than the
client side. When view states are being saved on the client side, it is serialized into a string and stored in a hidden
input field with the name javax.faces.ViewState. There are two performance penalties in doing this. First, there is an
overhead in serializing and deserializing the view state every time the view is being processed. Second, you will use
more bandwidth sending the state back and forth between the browser and server. The benefit of using client-side
saving of the state is that you minimize the memory footprint of the application. You should carefully consider what is
most critical for your application. Listing 11-2 shows the context parameter used to configure state saving in
/WEB-INF/web.xml .
Listing 11-2. Example of Enabling Server-Side View State Saving
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns=" http://xmlns.jcp.org/xml/ns/javaee "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd ">
...
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<!-- Replace server with client below to enable client-side state saving -->
<param-value>server</param-value>
</context-param>
...
</web-app>
Contexts and Dependency Injection (CDI)
When you develop JSF 2.x applications, you can choose between using the built-in managed bean scopes
( @RequestScoped , @SessionScoped , and @ViewScoped ) or use the Contexts and Dependency Injection (CDI) services
defined in JSR 299. CDI provides an architecture where all Java EE components (Servlets, Enterprise JavaBeans,
managed beans) follow the same programming model and lifecycle with well-defined scopes. It allows for Java EE
components to be loosely coupled and injected where needed. CDI has proven to be such a success that the built-in
managed scopes will be deprecated in future versions of JSF. If you start working on a new application you should use
the CDI services from the beginning. If you are working on an existing application that you must continue to maintain,
you should start planning a migration from the built-in managed bean scopes to CDI scopes.
Enabling CDI in JSF is simple. Create /WEB-INF/beans.xml specifying how CDI beans should be discovered as
illustrated in Listing 11-3. Once the file is created, you can start using CDI Scopes in your application.
Listing 11-3. /WEB-INF/beans.xml Enabling CDI in Your JSF Application
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" http://xmlns.jcp.org/xml/ns/javaee "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd "
bean-discovery-mode="annotated">
</beans>
Do not attempt to mix and match CDI scopes and the built-in JSF managed bean scopes. It will end up
causing confusion when your JSF beans start misbehaving because they are active in different scopes.
Caution
 
 
 
Search WWH ::




Custom Search