TechnoBuzz

A Techno Blog, mainly about Java

Spring Web Flow Beginner – Part Two

As a total beginner to Spring Web Flow, the main thing I have noticed in going through the material is that there is quite a difference between the 1.x and the new release 2.0.

Web Flow | Spring Web Flow Forum

There seems to be in package org.springframework.webflow.action a FlowAction and a FlowExecutor class. As well, the ExternalContext class which is credited to allowing WF to be decoupled from Servlet API .

I do not see a FlowAction or FlowExecutor in org.spring.webflow.action package like in webflow 1.

Instead, lets look at what we have from the example Spring Flow 2.0 Example application.

To use annotations for the transactions within the example, we do the following:

  1. Put an tag tx:annotation-driven in the spring config file
  2. Put @Transactional annotation in the services classes

@Transactional – The @Transactional annotation may be placed before an interface definition, a method on an interface, a class definition, or a public method on a class. However, please note that the mere presence of the @Transactional annotation is not enough to actually turn on the transactional behavior.

You can actually omit the ‘transaction-manager’ attribute in the <tx:annotation-driven/> tag if the bean name of the PlatformTransactionManager that you want to wire in has the name ‘transactionManager’.

<!– Instructs Spring to perfrom declarative transaction managemenet on annotated classes –>
<tx:annotation-driven/>

<!– Drives transactions using local JPA APIs –>
<bean id=”transactionManager” class=”org.springframework.orm.jpa.JpaTransactionManager”/>

The LocalContainerEntityManagerFactoryBean can be configured with all Persistent Unit information like is done here.



@PersistenceUnit – annotated on EntityManagerFactory instances are thread-safe

<!– Creates a EntityManagerFactory for use with the Hibernate JPA provider and a simple in-memory data source populated with test data –>
<bean id=”entityManagerFactory” class=”org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean”>
<property name=”jpaVendorAdapter”>
<bean class=”org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter”/>
</property>
</bean>

<!– Deploys a in-memory datasource –>
<bean id=”dataSource” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>
<property name=”driverClassName” value=”org.hsqldb.jdbcDriver”/>
<property name=”url” value=”jdbc:hsqldb:mem:tutorialSwf”/>
<property name=”username” value=”sa”/>
<property name=”password” value=””/>
</bean>

<!– Activates annotation-based bean configuration –>
<context:annotation-config/>

Mapping URLs to Handlers – maps request URLs to handlers. A simple way to create URL mapping rules is to define one as follows:

<!– Maps request URIs to controllers –>
<bean class=”org.springframework.web.servlet.handler.SimpleUrlHandlerMapping”>
<property name=”mappings”>
<props>
<prop key=”/f/*”>flowController</prop>
</props>
</property>
<property name=”defaultHandler”>
<!– Selects view names to render based on the request URI: e.g. /main selects “main” –>
<bean class=”org.springframework.web.servlet.mvc.UrlFilenameViewController”/>
</property>
</bean>

The Flow Handler manages executions of a single flow definition. Above the handler selects view based on the URI in a default handler. To Implement one, extend AbstractFlowHandler.

Flow Controller – The FlowHandler MVC integration approach, you define one handler per flow. This is overkill in the cases where default flow handling rules are sufficient. Web controller for the Spring web MVC framework that routes incoming requests to one or more managed web flows. Requests into the web flow system are managed using a configurable ServletFlowExecutionManager.

<!– Handles requests mapped to the Spring Web Flow system –>
<bean id=”flowController” class=”org.springframework.webflow.mvc.servlet.FlowController“/>

The flow executor is the core Web Flow configuration element. Flow execution listeners are also defined in the flow executor.

<!– Executes flows: the central entry point into the Spring Web Flow system –>
<webflow:flow-executor id=”flowExecutor”>
<webflow:flow-execution-listeners>
<webflow:listener ref=”jpaFlowExecutionListener”/>
</webflow:flow-execution-listeners>
</webflow:flow-executor>

FlowRegistry – placed where you register your flows

flow-builder-services attribute – customize the services used to build the flows in a registry…When the tag is defined, you only need to reference the services you want to customize.

<!– The registry of executable flow definitions –>
<webflow:flow-registry id=”flowRegistry” flow-builder-services=”facesFlowBuilderServices”>
<webflow:flow-location-pattern value=”/WEB-INF/flows/**/*.xml”/>
</webflow:flow-registry>

flow scoped persistence context – provides isolation of intermediate edits by only committing changes to the database at the end of flow execution. This pattern is often used in conjunction with an optimistic locking strategy to protect the integrity of data modified in parallel by multiple users.

@PersistenceContext – annotated on EntityManager instances are not thread safe

<flow xmlns=”http://www.springframework.org/schema/webflow&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd”&gt;
<persistence-context/>

configure the correct FlowExecutionListener in this case for JPA

<!– Installs a listener that manages JPA persistence contexts for flows that require them –>
<bean id=”jpaFlowExecutionListener” class=”org.springframework.webflow.persistence.JpaFlowExecutionListener”>
<constructor-arg ref=”entityManagerFactory”/>
<constructor-arg ref=”transactionManager”/>
</bean>

Here is how they handle the view:

<!– Maps logical view names to Facelet templates (e.g. ‘search’ to ‘/WEB-INF/search.xhtml’ –>
<bean id=”faceletsViewResolver” class=”org.springframework.web.servlet.view.UrlBasedViewResolver”>
<property name=”viewClass” value=”org.springframework.faces.mvc.JsfView”/>
<property name=”prefix” value=”/WEB-INF/”/>
<property name=”suffix” value=”.xhtml”/>
</bean>

Or a simple ViewResolver

Advertisements

July 19, 2008 Posted by | J2EE | 3 Comments