el.md 12.4 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1 2
# 4. Expression Language (EL)

茶陵後's avatar
茶陵後 已提交
3
## 4.1. Introduction
茶陵後's avatar
茶陵後 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Web Flow uses EL to access its data model and to invoke actions.
This chapter will familiarize you with EL syntax, configuration, and special EL variables you can reference from your flow definition.

EL is used for many things within a flow including:

1. Access client data such as declaring flow inputs or referencing request parameters.

2. Access data in Web Flow's `RequestContext` such as `flowScope` or `currentEvent`.

3. Invoke methods on Spring-managed objects through actions.

4. Resolve expressions such as state transition criteria, subflow ids, and view names.

EL is also used to bind form parameters to model objects and reversely to render formatted form fields from the properties of a model object.
That however does not apply when using Web Flow with JSF in which case the standard JSF component lifecyle applies.

茶陵後's avatar
茶陵後 已提交
21
### 4.1.1. Expression types
茶陵後's avatar
茶陵後 已提交
22 23 24

An important concept to understand is there are two types of expressions in Web Flow: standard expressions and template expressions.

茶陵後's avatar
茶陵後 已提交
25
#### Standard Expressions
茶陵後's avatar
茶陵後 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

The first and most common type of expression is the *standard expression*.
Such expressions are evaluated directly by the EL and need not be enclosed in delimiters like `#{}`.
For example:

```
<evaluate expression="searchCriteria.nextPage()" />
				
```

The expression above is a standard expression that invokes the `nextPage` method on the `searchCriteria` variable when evaluated.
If you attempt to enclose this expression in a special delimiter like `#{}` you will get an `IllegalArgumentException`.
In this context the delimiter is seen as redundant.
The only acceptable value for the `expression` attribute is an single expression string.

茶陵後's avatar
茶陵後 已提交
41
#### Template expressions
茶陵後's avatar
茶陵後 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

The second type of expression is a *template expression*.
A template expression allows mixing of literal text with one or more standard expressions.
Each standard expression block is explicitly surrounded with the `#{}` delimiters.
For example:

```
<view-state id="error" view="error-#{externalContext.locale}.xhtml" />
				
```

The expression above is a template expression.
The result of evaluation will be a string that concatenates literal text such as `error-` and `.xhtml` with the result of evaluating `externalContext.locale`.
As you can see, explicit delimiters are necessary here to demarcate standard expression blocks within the template.

|                                                                                                                                               ![[Note]](images/note.png)                                                                                                                                               |Note|
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:---|
|See the Web Flow XML schema for a complete listing of those XML attributes that accept standard expressions and those that accept template expressions.<br/>You can also use F2 in Eclipse (or equivalent shortcut in other IDEs) to access available documentation when typing out specific flow definition attributes.|    |

茶陵後's avatar
茶陵後 已提交
61
## 4.2. EL Implementations
茶陵後's avatar
茶陵後 已提交
62

茶陵後's avatar
茶陵後 已提交
63
### 4.2.1. Spring EL
茶陵後's avatar
茶陵後 已提交
64 65 66 67 68

Web Flow uses the [Spring Expression Language](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html) (Spring EL).
Spring EL was created to provide a single, well-supported expression language for use across all the products in the Spring portfolio.
It is distributed as a separate jar `org.springframework.expression` in the Spring Framework.

茶陵後's avatar
茶陵後 已提交
69
### 4.2.2. Unified EL
茶陵後's avatar
茶陵後 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

Use of [Unified EL](https://en.wikipedia.org/wiki/Unified_Expression_Language)also implies a dependency on `el-api` although that is typically *provided*by your web container.
Although Spring EL is the default and recommended expression language to use,
it is possible to replace it with Unified EL if you wish to do so.
You need the following Spring configuration to plug in the `WebFlowELExpressionParser` to the `flow-builder-services`:

```
<webflow:flow-builder-services expression-parser="expressionParser"/>

<bean id="expressionParser" class="org.springframework.webflow.expression.el.WebFlowELExpressionParser">
    <constructor-arg>
        <bean class="org.jboss.el.ExpressionFactoryImpl" />
    </constructor-arg>
</bean>

```

Note that if your application is registering custom converters it's important to ensure the WebFlowELExpressionParser is configured with the conversion service that has those custom converters.

```
<webflow:flow-builder-services expression-parser="expressionParser" conversion-service="conversionService"/>

<bean id="expressionParser" class="org.springframework.webflow.expression.el.WebFlowELExpressionParser">
    <constructor-arg>
        <bean class="org.jboss.el.ExpressionFactoryImpl" />
    </constructor-arg>
    <property name="conversionService" ref="conversionService"/>
</bean>

<bean id="conversionService" class="somepackage.ApplicationConversionService"/>

```

茶陵後's avatar
茶陵後 已提交
103
## 4.3. EL portability
茶陵後's avatar
茶陵後 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

In general, you will find Spring EL and Unified EL to have a very similar syntax.

Note however there are some advantages to Spring EL.
For example Spring EL is closely integrated with the type conversion of Spring 3 and that allows you to take full advantage of its features.
Specifically the automatic detection of generic types as well as the use of formatting annotations is currently supported with Spring EL only.

There are some minor changes to keep in mind when upgrading to Spring EL from Unified EL as follows:

1. Expressions deliniated with `${}` in flow definitions must be changed to `#{}`.

2. Expressions testing the current event `#{currentEvent == 'submit'}` must be changed to `#{currentEvent.id == 'submit'}`.

3. Resolving properties such as `#{currentUser.name}` may cause NullPointerException without any checks such as `#{currentUser != null ? currentUser.name : null}`.
   A much better alternative though is the safe navigation operator `#{currentUser?.name}`.

For more information on Spring EL syntax please refer to the [Language Reference](http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html#expressions-language-ref) section in the Spring Documentation.

茶陵後's avatar
茶陵後 已提交
122
## 4.4. Special EL variables
茶陵後's avatar
茶陵後 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

There are several implicit variables you may reference from within a flow.
These variables are discussed in this section.

Keep in mind this general rule.
Variables referring to data scopes (flowScope, viewScope, requestScope, etc.) should only be used when assigning a new variable to one of the scopes.

For example when assigning the result of the call to `bookingService.findHotels(searchCriteria)` to a new variable called "hotels" you must prefix it with a scope variable in order to let Web Flow know where you want it stored:

```
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow" ... >

	<var name="searchCriteria" class="org.springframework.webflow.samples.booking.SearchCriteria" />

	<view-state id="reviewHotels">
		<on-render>
			<evaluate expression="bookingService.findHotels(searchCriteria)" result="viewScope.hotels" />
		</on-render>
	</view-state>

</flow>
			
```

However when setting an existing variable such as "searchCriteria" in the example below, you reference the variable directly without prefixing it with any scope variables:

```
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow" ... >

	<var name="searchCriteria" class="org.springframework.webflow.samples.booking.SearchCriteria" />

	<view-state id="reviewHotels">
		<transition on="sort">
			<set name="searchCriteria.sortBy" value="requestParameters.sortBy" />
		</transition>
	</view-state>

</flow>
			
```

The following is the list of implicit variables you can reference within a flow definition:

茶陵後's avatar
茶陵後 已提交
168
### 4.4.1. flowScope
茶陵後's avatar
茶陵後 已提交
169 170 171 172 173 174 175 176 177 178

Use `flowScope` to assign a flow variable.
Flow scope gets allocated when a flow starts and destroyed when the flow ends. With the default
implementation, any objects stored in flow scope need to be Serializable.

```
<evaluate expression="searchService.findHotel(hotelId)" result="flowScope.hotel" />
			
```

茶陵後's avatar
茶陵後 已提交
179
### 4.4.2. viewScope
茶陵後's avatar
茶陵後 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193

Use `viewScope` to assign a view variable.
View scope gets allocated when a `view-state` enters and destroyed when the state exits.
View scope is *only* referenceable from within a `view-state`. With the
default implementation, any objects stored in view scope need to be Serializable.

```
<on-render>
    <evaluate expression="searchService.findHotels(searchCriteria)" result="viewScope.hotels"
              result-type="dataModel" />
</on-render>
			
```

茶陵後's avatar
茶陵後 已提交
194
### 4.4.3. requestScope
茶陵後's avatar
茶陵後 已提交
195 196 197 198 199 200 201 202 203

Use `requestScope` to assign a request variable.
Request scope gets allocated when a flow is called and destroyed when the flow returns.

```
<set name="requestScope.hotelId" value="requestParameters.id" type="long" />
			
```

茶陵後's avatar
茶陵後 已提交
204
### 4.4.4. flashScope
茶陵後's avatar
茶陵後 已提交
205 206 207 208 209 210 211 212 213 214

Use `flashScope` to assign a flash variable.
Flash scope gets allocated when a flow starts, cleared after every view render, and destroyed when the
flow ends. With the default implementation, any objects stored in flash scope need to be Serializable.

```
<set name="flashScope.statusMessage" value="'Booking confirmed'" />
			
```

茶陵後's avatar
茶陵後 已提交
215
### 4.4.5. conversationScope
茶陵後's avatar
茶陵後 已提交
216 217 218 219 220 221 222 223 224 225 226 227

Use `conversationScope` to assign a conversation variable.
Conversation scope gets allocated when a top-level flow starts and destroyed when the top-level flow ends.
Conversation scope is shared by a top-level flow and all of its subflows. With the default
implementation, conversation scoped objects are stored in the HTTP session and should generally be
Serializable to account for typical session replication.

```
<evaluate expression="searchService.findHotel(hotelId)" result="conversationScope.hotel" />
			
```

茶陵後's avatar
茶陵後 已提交
228
### 4.4.6. requestParameters
茶陵後's avatar
茶陵後 已提交
229 230 231 232 233 234 235 236

Use `requestParameters` to access a client request parameter:

```
<set name="requestScope.hotelId" value="requestParameters.id" type="long" />
			
```

茶陵後's avatar
茶陵後 已提交
237
### 4.4.7. currentEvent
茶陵後's avatar
茶陵後 已提交
238 239 240 241 242 243 244 245

Use `currentEvent` to access attributes of the current `Event`:

```
<evaluate expression="booking.guests.add(currentEvent.attributes.guest)" />
			
```

茶陵後's avatar
茶陵後 已提交
246
### 4.4.8. currentUser
茶陵後's avatar
茶陵後 已提交
247 248 249 250 251 252 253 254 255

Use `currentUser` to access the authenticated `Principal`:

```
<evaluate expression="bookingService.createBooking(hotelId, currentUser.name)"
          result="flowScope.booking" />
			
```

茶陵後's avatar
茶陵後 已提交
256
### 4.4.9. messageContext
茶陵後's avatar
茶陵後 已提交
257 258 259 260 261 262 263 264 265

Use `messageContext` to access a context for retrieving and creating flow execution messages, including error and success messages.
See the `MessageContext` Javadocs for more information.

```
<evaluate expression="bookingValidator.validate(booking, messageContext)" />
			
```

茶陵後's avatar
茶陵後 已提交
266
### 4.4.10. resourceBundle
茶陵後's avatar
茶陵後 已提交
267 268 269 270 271 272 273 274

Use `resourceBundle` to access a message resource.

```
<set name="flashScope.successMessage" value="resourceBundle.successMessage" />
			
```

茶陵後's avatar
茶陵後 已提交
275
### 4.4.11. flowRequestContext
茶陵後's avatar
茶陵後 已提交
276 277 278 279

Use `flowRequestContext` to access the `RequestContext` API, which is a representation of the current flow request.
See the API Javadocs for more information.

茶陵後's avatar
茶陵後 已提交
280
### 4.4.12. flowExecutionContext
茶陵後's avatar
茶陵後 已提交
281 282 283 284

Use `flowExecutionContext` to access the `FlowExecutionContext` API, which is a representation of the current flow state.
See the API Javadocs for more information.

茶陵後's avatar
茶陵後 已提交
285
### 4.4.13. flowExecutionUrl
茶陵後's avatar
茶陵後 已提交
286 287 288

Use `flowExecutionUrl` to access the context-relative URI for the current flow execution view-state.

茶陵後's avatar
茶陵後 已提交
289
### 4.4.14. externalContext
茶陵後's avatar
茶陵後 已提交
290 291 292 293 294 295 296 297 298 299

Use `externalContext` to access the client environment, including user session attributes.
See the `ExternalContext` API JavaDocs for more information.

```
<evaluate expression="searchService.suggestHotels(externalContext.sessionMap.userProfile)"
          result="viewScope.hotels" />
			
```

茶陵後's avatar
茶陵後 已提交
300
## 4.5. Scope searching algorithm
茶陵後's avatar
茶陵後 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

As mentioned earlier in this section when assigning a variable in one of the flow scopes, referencing that scope is required.
For example:

```
<set name="requestScope.hotelId" value="requestParameters.id" type="long" />
		
```

When simply accessing a variable in one of the scopes, referencing the scope is optional.
For example:

```
<evaluate expression="entityManager.persist(booking)" />
		
```

When no scope is specified, like in the use of `booking` above, a scope searching algorithm is used.
The algorithm will look in request, flash, view, flow, and conversation scope for the variable.
If no such variable is found, an `EvaluationException` will be thrown.