Sie sind auf Seite 1von 13

Integrating Spring Web Flow with JSF

1 of 13

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

Follow @javabeat

Like

Tweet

also read:

Advertise Here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

<?xml version="1.0" encoding="ISO-8859-1"?>


<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/webapp_2_4.xsd"
version="2.4">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/spring-fc-config.xml
</param-value>
</context-param>

<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>1</param-value>
</context-param>
<filter>
<filter-name>charEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>

08-07-2015 23:06

Integrating Spring Web Flow with JSF

2 of 13

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Resources Servlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resources Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/faces
http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<webflow:flow-executor id="flowExecutor">
</webflow:flow-executor>
<webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices">
<webflow:flow-location path="/WEB-INF/flows/test.xml" />
</webflow:flow-registry>
<faces:flow-builder-services id="facesFlowBuilderServices" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/test=flowController
</value>

08-07-2015 23:06

Integrating Spring Web Flow with JSF

3 of 13

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

1
2
3
4
5
6
7
8
9
10
11

</property>
<property name="defaultHandler">
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
</property>
</bean>
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>
<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>
</beans>

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<application>
<!-- Enables Facelets -->
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
</faces-config>

1
2
3
4
5

<html>
<head>
<meta http-equiv="Refresh" content="0; URL=spring/start">
</head>
</html>

1
2
3
4

<?xml version="1.0" encoding="UTF-8"?>


<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

5
6
7
8
9

1
2
3
4
5

1
2
3

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

<view-state id="test">
</view-state>
</flow>

<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>

<?xml version='1.0' encoding='UTF-8' ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1
/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"

08-07-2015 23:06

Integrating Spring Web Flow with JSF

4 of 13

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

1
2
3
4
5
6
7
8
9

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<body>
<ui:composition template="../template.xhtml">
<ui:define name="heading">
</ui:define>
<ui:define name="body">
Sample page which illustrates the integration between JSF and Spring Web Flow
</ui:define>
</ui:composition>
</body>
</html>

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org


/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:sf="http://www.springframework.org/tags/faces"
template="template.xhtml">
<ui:define name="heading">
</ui:define>
<ui:define name="body">
<div align="left">
<p align="left">
<a href="test">Click here to invoke the Facelets page</a>
</p>
</div>
</ui:define>
</ui:composition>

<?xml version="1.0" encoding="UTF-8"?>


<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<var name="employeeSearchCriteria"
class="net.javabeat.spring.webflow.jsf.search.employee.EmployeeCriteria" />
<view-state id="showSearchPage">
<transition on="searchEmployee" to="showSearchResults">

08-07-2015 23:06

Integrating Spring Web Flow with JSF

5 of 13

10
11
12
13
14
15
16
17
18
19
20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

</transition>
</view-state>
<view-state id="showSearchResults">
<on-render>
<evaluate expression="employeeSearchService.findEmployees(employeeSearchCriteria)"
result="viewScope.allEmployees" result-type="dataModel" />
</on-render>
</view-state>
</flow>

<?xml version='1.0' encoding='UTF-8' ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1
/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:sf="http://www.springframework.org/tags/faces">
<body>
<ui:composition template="../template.xhtml">
<ui:define name="heading">
<p>Employee Search Page</p>
</ui:define>
<ui:define name="body">
<h:form id="mainForm">
<h:outputLabel for="searchString">Search Employee:</h:outputLabel>
<sf:clientTextValidator promptMessage="Search employees by name">
<h:inputText id="empName" value="#{employeeSearchCriteria.empName}" />
</sf:clientTextValidator>
<br/><br/>
<sf:commandButton id="searchEmployees" value="Find Employees"
processIds="*" action="searchEmployee" />
</h:form>
</ui:define>
</ui:composition>
</body>
</html>

<?xml version='1.0' encoding='UTF-8' ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1
/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:sf="http://www.springframework.org/tags/faces">
<body>
<ui:composition template="../template.xhtml">
<ui:define name="heading">
<h2>Search Results</h2>
</ui:define>
<ui:define name="body">
<h:dataTable id="allEmployees" var="employee" value="#{allEmployees}"
cellspacing="0" cellpadding="0" border="1">
<h:column>
<f:facet name="header">Emp Id</f:facet>
<h:outputText value="#{employee.id}" align="left" />
</h:column>
<h:column>

08-07-2015 23:06

Integrating Spring Web Flow with JSF

6 of 13

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

<f:facet name="header">Name</f:facet>
<h:outputText value="#{employee.name}" align="left" />
</h:column>
<h:column>
<f:facet name="header">Date of Birth</f:facet>
<h:outputText value="#{employee.dob}" align="left" />
</h:column>
<h:column>
<f:facet name="header">Designation</f:facet>
<h:outputText value="#{employee.designation}" align="left" />
</h:column>
</h:dataTable>
<p align="left">
<a href="search">Search Again</a>
</p>
</ui:define>
</ui:composition>
</body>
</html>

package net.javabeat.spring.webflow.jsf.search.employee;
import java.io.Serializable;
import java.util.Date;
public class Employee implements Serializable{
private
private
private
private

String id;
String name;
Date dob;
String designation;

public String getDesignation() {


return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

08-07-2015 23:06

Integrating Spring Web Flow with JSF

7 of 13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

package net.javabeat.spring.webflow.jsf.search.employee;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

package net.javabeat.spring.webflow.jsf.search.employee;

39
40
41
42
43
44
45
46
47
48
49
50

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

import java.io.Serializable;
public class EmployeeCriteria implements Serializable{
private static final long serialVersionUID = 1L;
private String empName;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class EmployeeSearchService {
private static List<Employee> allEmployees;
public List<Employee> findEmployees(EmployeeCriteria criteria){
List<Employee> searchedEmployees = new ArrayList<Employee>();
if (criteria == null){
return allEmployees;
}
String searchEmpName = criteria.getEmpName();
if (searchEmpName != null && searchEmpName.trim().length() > 0){
for (Employee anEmployee : allEmployees){
if (anEmployee.getName().contains(searchEmpName)){
searchedEmployees.add(anEmployee);
}
}
return searchedEmployees;
}else{
return allEmployees;
}
}
static{
allEmployees = new ArrayList<Employee>();
allEmployees.add(employee("10000", "Steve Clark", new Date(1960, 6, 12), "Employee"));
allEmployees.add(employee("10000", "Alfred Ray", new Date(1954, 4, 17), "Manager"));
allEmployees.add(employee("10000", "Robert Woulsh", new Date(1944, 2, 16),
"Director"));
}
static Employee employee(String id, String name, Date dob, String designation){
Employee employee = new Employee();
employee.setId(id);
employee.setName(name);
employee.setDob(dob);
employee.setDesignation(designation);
return employee;
}
}

08-07-2015 23:06

Integrating Spring Web Flow with JSF

8 of 13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/faces
http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="net.javabeat.spring.webflow.jsf.book" />
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="jpaFlowExecutionListener" />
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices">
<webflow:flow-location path="/WEB-INF/flows/bookMain.xml" />
</webflow:flow-registry>
<faces:flow-builder-services id="facesFlowBuilderServices" />
<bean id="jpaFlowExecutionListener"
class="org.springframework.webflow.persistence.JpaFlowExecutionListener">
<constructor-arg ref="entityManagerFactory" />
<constructor-arg ref="transactionManager" />
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/bookMain=flowController
/bookSearchPage=flowController
/bookAdd=flowController
</value>
</property>
<property name="defaultHandler">
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
</property>

08-07-2015 23:06

Integrating Spring Web Flow with JSF

9 of 13

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

</bean>
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>
<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>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:book" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
</beans>

<?xml version="1.0" encoding="UTF-8"?>


<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com
/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="bookDatabase">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>net.javabeat.spring.webflow.jsf.book.Book</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.cache.provider_class"
value="org.hibernate.cache.HashtableCacheProvider"/>
</properties>
</persistence-unit>
</persistence>

package net.javabeat.spring.webflow.jsf.book;
import
import
import
import
import

java.io.Serializable;
javax.persistence.Entity;
javax.persistence.GeneratedValue;
javax.persistence.GenerationType;
javax.persistence.Id;

@Entity
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String category;
private String author;
private double price;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;

08-07-2015 23:06

Integrating Spring Web Flow with JSF

10 of 13

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Book)) {
return false;
}
Book other = (Book) object;
if ((this.id == null && other.id != null) || (this.id != null &&
!this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "net.javabeat.spring.webflow.jsf.book.Book[id=" + id + "]";
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13

package net.javabeat.spring.webflow.jsf.book;

1
2
3
4
5
6
7
8

package net.javabeat.spring.webflow.jsf.book;

import java.util.List;
public interface BookService {
List<Book> findAllBooks();
List<Book> findBooks(BookSearchCriteria criteria);
void createBook(String name, String category, String author, double price);
}

import
import
import
import
import
import

java.util.List;
javax.persistence.EntityManager;
javax.persistence.PersistenceContext;
javax.persistence.Query;
org.springframework.stereotype.Repository;
org.springframework.stereotype.Service;

08-07-2015 23:06

Integrating Spring Web Flow with JSF

11 of 13

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

1
2
3
4
5
6
7
8
9
10
11
12

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

import org.springframework.transaction.annotation.Transactional;
@Service("bookService")
@Repository
public class BookServiceImpl implements BookService {
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em){
this.em = em;
}
@Transactional(readOnly=true)
public List<Book> findAllBooks() {
Query query = em.createQuery("select book from Book book");
List<Book> results = query.getResultList();
if (results == null || results.size() == 0){
createTestBooks();
return query.getResultList();
}else{
return results;
}
}
private void createTestBooks(){
createBook("Java Programming", "Technical", "James", 1110.00);
createBook("Life After Death", "Spiritual", "Unknown", 3140.00);
createBook("All About Numerology", "Numerolgy", "Richard", 6130.00);
}
@Transactional(readOnly=true)
public List<Book> findBooks(BookSearchCriteria criteria) {
if (criteria == null){
return findAllBooks();
}
String name = criteria.getName();
String author = criteria.getAuthor();
String query = "select book from Book";
if (name != null && name.trim().length() > 0){
query = query + " where upper(book.name) = :name ";
}
if (author != null && author.trim().length() > 0){
if (query.contains("where")){
query = query + " and ";
}else{
query = query + "where ";
}
query = query + " upper(book.author) = :author ";
}
Query queryObject = em.createQuery(query);
queryObject.setParameter("name", name);
queryObject.setParameter("author", author);
List<Book> results = queryObject.getResultList();
if (results == null || results.size() == 0){
createTestBooks();
}
return queryObject.getResultList();
}
@Transactional(readOnly=true)
public void createBook(String name, String category, String author, double price) {
Book book = new Book();
book.setName(name);
book.setCategory(category);
book.setAuthor(author);
book.setPrice(price);
em.persist(book);
}
}

<?xml version="1.0" encoding="UTF-8"?>


<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<var name="bookSearchCriteria"
class="net.javabeat.spring.webflow.jsf.book.BookSearchCriteria" />
<view-state id="bookMain">
<on-render>
<evaluate expression="bookService.findAllBooks()" result="viewScope.allBooks"
result-type="dataModel" />
</on-render>
<transition on="addBooks" to="bookAdd">

08-07-2015 23:06

Integrating Spring Web Flow with JSF

12 of 13

13
14
15
16
17
18
19
20
21
22
23

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

</transition>
</view-state>
<view-state id="bookAdd">
<on-render>
<evaluate expression="bookService.createBook(bookSearchCriteria.name,
bookSearchCriteria.category,
bookSearchCriteria.author, bookSearchCriteria.price)"/>
</on-render>
</view-state>
</flow>

1 Comment

Sort by

Add a comment...

Rafael Ruiz Tabares Bristol, United Kingdom


Very nice work! Thank you for writing this article. Now I'm developing
a webapp on hibernate,spring core, security, social,webflow and jsf2,
primefaces, prettyfaces. And I can say that it's a puzzle!
Like Reply Feb 12, 2014 3:16pm
Facebook Comments Plugin

Unify your ALM tools now


Are your applications working in silos? ConnectALL unifies
them!

08-07-2015 23:06

Integrating Spring Web Flow with JSF

13 of 13

http://www.javabeat.net/integrating-spring-web-flow-with-jsf/

Lifestyle Journal

Unique Method Regrows Lost Hair


(Do This Daily)

Web2Carz.com

5 Worst Shift Knobs Ever

TheFix.com

Ultimate Guide to Rehab


JAVABEAT

What is the difference between JRE,JVM and


JDK?

Quartz Job Scheduler Example Code

Spring 5 Announced

Quartz Scheduler Tutorial

0 Comments

08-07-2015 23:06

Das könnte Ihnen auch gefallen