1
1
[[web-integration]]
2
2
= Other Web Frameworks
3
3
4
-
5
-
6
-
7
- [[intro]]
8
- == Introduction
9
-
10
4
This chapter details Spring's integration with third party web frameworks.
11
5
12
6
One of the core value propositions of the Spring Framework is that of enabling
13
- __choice__ . In a general sense, Spring does not force one to use or buy into any
7
+ _choice_ . In a general sense, Spring does not force you to use or buy into any
14
8
particular architecture, technology, or methodology (although it certainly recommends
15
9
some over others). This freedom to pick and choose the architecture, technology, or
16
10
methodology that is most relevant to a developer and their development team is
17
11
arguably most evident in the web area, where Spring provides its own web framework
18
- (<<mvc,Spring MVC>>), while at the same time providing integration with a number of
12
+ (<<mvc,Spring MVC>>) while, at the same time, providing integration with a number of
19
13
popular third party web frameworks.
20
14
21
15
22
16
23
-
24
17
[[web-integration-common]]
25
- == Common config
18
+ == Common Configuration
26
19
Before diving into the integration specifics of each supported web framework, let us
27
- first take a look at the Spring configuration that is __not__ specific to any one web
20
+ first take a look at the Spring configuration that is not specific to any one web
28
21
framework. (This section is equally applicable to Spring's own web framework, Spring
29
22
MVC.)
30
23
31
- One of the concepts (for want of a better word) espoused by ( Spring's) lightweight
32
- application model is that of a layered architecture. Remember that in a ' classic'
33
- layered architecture, the web layer is but one of many layers; it serves as one of the
34
- entry points into a server side application and it delegates to service objects
35
- (facades) defined in a service layer to satisfy business specific (and
24
+ One of the concepts (for want of a better word) espoused by Spring's lightweight
25
+ application model is that of a layered architecture. Remember that in a "` classic`"
26
+ layered architecture, the web layer is but one of many layers. It serves as one of the
27
+ entry points into a server- side application, and it delegates to service objects
28
+ (facades) that are defined in a service layer to satisfy business- specific (and
36
29
presentation-technology agnostic) use cases. In Spring, these service objects, any other
37
- business-specific objects, data access objects, etc. exist in a distinct ' business
38
- context' , which contains __no__ web or presentation layer objects (presentation objects
39
- such as Spring MVC controllers are typically configured in a distinct ' presentation
40
- context' ). This section details how one configures a Spring container (a
41
- `WebApplicationContext`) that contains all of the 'business beans' in one's application.
30
+ business-specific objects, data- access objects, and others exist in a distinct "` business
31
+ context`" , which contains no web or presentation layer objects (presentation objects
32
+ , such as Spring MVC controllers, are typically configured in a distinct "` presentation
33
+ context`" ). This section details how you can configure a Spring container (a
34
+ `WebApplicationContext`) that contains all of the 'business beans' in your application.
42
35
43
- On to specifics: all that one need do is to declare a
36
+ Moving on to specifics, all you one need to do is declare a
44
37
{api-spring-framework}/web/context/ContextLoaderListener.html[`ContextLoaderListener`]
45
- in the standard Java EE servlet `web.xml` file of one's web application, and add a
38
+ in the standard Java EE servlet `web.xml` file of your web application and add a
46
39
`contextConfigLocation`<context-param/> section (in the same file) that defines which
47
40
set of Spring XML configuration files to load.
48
41
49
- Find below the <listener/> configuration:
42
+ Consider the following ` <listener/>` configuration:
50
43
44
+ ====
51
45
[source,xml,indent=0]
52
46
[subs="verbatim,quotes"]
53
47
----
54
48
<listener>
55
49
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
56
50
</listener>
57
51
----
52
+ ====
58
53
59
- Find below the <context-param/> configuration:
54
+ Further consider the following ` <context-param/>` configuration:
60
55
56
+ ====
61
57
[source,xml,indent=0]
62
58
[subs="verbatim,quotes"]
63
59
----
@@ -66,30 +62,35 @@ Find below the <context-param/> configuration:
66
62
<param-value>/WEB-INF/applicationContext*.xml</param-value>
67
63
</context-param>
68
64
----
65
+ ====
69
66
70
- If you don't specify the `contextConfigLocation` context parameter, the
71
- `ContextLoaderListener` will look for a file called `/WEB-INF/applicationContext.xml` to
67
+ If you do not specify the `contextConfigLocation` context parameter, the
68
+ `ContextLoaderListener` looks for a file called `/WEB-INF/applicationContext.xml` to
72
69
load. Once the context files are loaded, Spring creates a
73
70
{api-spring-framework}/web/context/WebApplicationContext.html[`WebApplicationContext`]
74
71
object based on the bean definitions and stores it in the `ServletContext` of the web
75
72
application.
76
73
77
- All Java web frameworks are built on top of the Servlet API, and so one can use the
78
- following code snippet to get access to this ' business context' `ApplicationContext`
74
+ All Java web frameworks are built on top of the Servlet API, so you can use the
75
+ following code snippet to get access to this "` business context`" `ApplicationContext`
79
76
created by the `ContextLoaderListener`.
80
77
78
+ The following example shows how to get the `WebApplicationContext`:
79
+
80
+ ====
81
81
[source,java,indent=0]
82
82
[subs="verbatim,quotes"]
83
83
----
84
84
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
85
85
----
86
+ ====
86
87
87
88
The
88
89
{api-spring-framework}/web/context/support/WebApplicationContextUtils.html[`WebApplicationContextUtils`]
89
- class is for convenience, so you don't have to remember the name of the `ServletContext`
90
- attribute. Its __getWebApplicationContext()__ method will return `null` if an object
91
- doesn't exist under the `WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE`
92
- key. Rather than risk getting `NullPointerExceptions` in your application, it's better
90
+ class is for convenience, so you need not remember the name of the `ServletContext`
91
+ attribute. Its `getWebApplicationContext()` method returns `null` if an object
92
+ does not exist under the `WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE`
93
+ key. Rather than risk getting `NullPointerExceptions` in your application, it is better
93
94
to use the `getRequiredWebApplicationContext()` method. This method throws an exception
94
95
when the `ApplicationContext` is missing.
95
96
@@ -99,45 +100,44 @@ their implemented interfaces.
99
100
100
101
Fortunately, most of the frameworks in this section have simpler ways of looking up
101
102
beans. Not only do they make it easy to get beans from a Spring container, but they also
102
- allow you to use dependency injection on their controllers. Each web framework section
103
+ let you use dependency injection on their controllers. Each web framework section
103
104
has more detail on its specific integration strategies.
104
105
105
106
106
107
107
-
108
108
[[jsf]]
109
109
== JSF
110
+
110
111
JavaServer Faces (JSF) is the JCP's standard component-based, event-driven web user
111
112
interface framework. As of Java EE 5, it is an official part of the Java EE umbrella.
112
113
113
114
For a popular JSF runtime as well as for popular JSF component libraries, check out the
114
115
http://myfaces.apache.org/[Apache MyFaces project]. The MyFaces project also provides
115
- common JSF extensions such as http://myfaces.apache.org/orchestra/[MyFaces Orchestra]:
116
- a Spring-based JSF extension that provides rich conversation scope support.
116
+ common JSF extensions, such as http://myfaces.apache.org/orchestra/[MyFaces Orchestra]
117
+ ( a Spring-based JSF extension that provides rich conversation scope support) .
117
118
118
- [NOTE]
119
- ====
120
- Spring Web Flow 2.0 provides rich JSF support through its newly established Spring Faces
119
+ NOTE: Spring Web Flow 2.0 provides rich JSF support through its newly established Spring Faces
121
120
module, both for JSF-centric usage (as described in this section) and for Spring-centric
122
- usage (using JSF views within a Spring MVC dispatcher). Check out the
123
- http://projects.spring.io/spring-webflow[Spring Web Flow website] for details!
124
- ====
121
+ usage (using JSF views within a Spring MVC dispatcher). See the
122
+ http://projects.spring.io/spring-webflow[Spring Web Flow website] for details.
125
123
126
124
The key element in Spring's JSF integration is the JSF `ELResolver` mechanism.
127
125
128
126
129
127
130
128
[[jsf-springbeanfaceselresolver]]
131
129
=== Spring Bean Resolver
130
+
132
131
`SpringBeanFacesELResolver` is a JSF 1.2+ compliant `ELResolver` implementation,
133
- integrating with the standard Unified EL as used by JSF 1.2 and JSP 2.1. Like
134
- `SpringBeanVariableResolver`, it delegates to the Spring's ' business context'
135
- `WebApplicationContext` __first__, then to the default resolver of the underlying JSF
132
+ integrating with the standard Unified EL as used by JSF 1.2 and JSP 2.1. As
133
+ `SpringBeanVariableResolver`, it delegates to Spring's "` business context`"
134
+ `WebApplicationContext` first and then to the default resolver of the underlying JSF
136
135
implementation.
137
136
138
- Configuration-wise, simply define `SpringBeanFacesELResolver` in your JSF
139
- __faces -context.xml__ file:
137
+ Configuration-wise, you can define `SpringBeanFacesELResolver` in your JSF
138
+ `faces -context.xml` file, as the following example shows :
140
139
140
+ ====
141
141
[source,xml,indent=0]
142
142
[subs="verbatim,quotes"]
143
143
----
@@ -148,33 +148,39 @@ __faces-context.xml__ file:
148
148
</application>
149
149
</faces-config>
150
150
----
151
+ ====
151
152
152
153
153
154
154
155
[[jsf-facescontextutils]]
155
- === FacesContextUtils
156
- A custom `VariableResolver` works well when mapping one's properties to beans
157
- in __faces-config.xml__, but at times one may need to grab a bean explicitly. The
156
+ === Using `FacesContextUtils`
157
+
158
+ A custom `VariableResolver` works well when mapping your properties to beans
159
+ in `faces-config.xml`, but, at times, you may need to explicitly grab a bean. The
158
160
{api-spring-framework}/web/jsf/FacesContextUtils.html[`FacesContextUtils`]
159
161
class makes this easy. It is similar to `WebApplicationContextUtils`, except that it
160
162
takes a `FacesContext` parameter rather than a `ServletContext` parameter.
161
163
164
+ The following example shows how to use `FacesContextUtils`:
165
+
166
+ ====
162
167
[source,java,indent=0]
163
168
[subs="verbatim,quotes"]
164
169
----
165
170
ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
166
171
----
167
-
172
+ ====
168
173
169
174
170
175
171
176
[[struts]]
172
177
== Apache Struts 2.x
173
- Invented by Craig McClanahan, http://struts.apache.org[Struts] is an open source project
178
+
179
+ Invented by Craig McClanahan, http://struts.apache.org[Struts] is an open-source project
174
180
hosted by the Apache Software Foundation. At the time, it greatly simplified the
175
181
JSP/Servlet programming paradigm and won over many developers who were using proprietary
176
- frameworks. It simplified the programming model, it was open source (and thus free as in
177
- beer), and it had a large community, which allowed the project to grow and become popular
182
+ frameworks. It simplified the programming model, it was open source (and thus free, as in
183
+ beer), and it had a large community, which let the project grow and become popular
178
184
among Java web developers.
179
185
180
186
Check out the Struts
@@ -183,28 +189,25 @@ built-in Spring integration shipped with Struts.
183
189
184
190
185
191
186
-
187
192
[[tapestry]]
188
193
== Tapestry 5.x
189
- From the http://tapestry.apache.org/[Tapestry homepage]:
190
194
191
- Tapestry is a "__Component oriented framework for creating dynamic, robust,
192
- highly scalable web applications in Java.__ "
195
+ http://tapestry.apache.org/[ Tapestry] is a ""Component oriented framework for creating dynamic, robust,
196
+ highly scalable web applications in Java." "
193
197
194
198
While Spring has its own <<mvc,powerful web layer>>, there are a number of unique
195
- advantages to building an enterprise Java application using a combination of Tapestry
199
+ advantages to building an enterprise Java application by using a combination of Tapestry
196
200
for the web user interface and the Spring container for the lower layers.
197
201
198
- For more information, check out Tapestry's dedicated
202
+ For more information, see Tapestry's dedicated
199
203
https://tapestry.apache.org/integrating-with-spring-framework.html[integration module for
200
204
Spring].
201
205
202
206
203
207
204
-
205
208
[[web-integration-resources]]
206
209
== Further Resources
207
- Find below links to further resources about the various web frameworks described in this
210
+ The following links go to further resources about the various web frameworks described in this
208
211
chapter.
209
212
210
213
* The http://www.oracle.com/technetwork/java/javaee/javaserverfaces-139869.html[JSF] homepage
0 commit comments