Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between #{} ${} and %{}?

I'm currently working with struts2, and I just don't understand what the difference is between ${var}, #{var}, and %{var} are they different scopes? what are they?

I found an example of the #:

<s:select label="Year"
      id="%{param.name}"
      list="#{'2010':'2010','2011':'2011','2012':'2012','2013':'2013','2014':'2014', '2015':'2015'}"
      value="%{currentYear}"
      required="true"
/>

here it looks like it's an associative array, but there's other times I've seen it as #var (without the brackets) any ideas?

like image 339
JKirchartz Avatar asked Feb 23 '12 21:02

JKirchartz


2 Answers

Just a another note:

You may use $ in your action to ask struts to evaluate your parameters against OGNL before passing it to other methods for example:

Using a custom validator

Struts 2 - reusing Custom Expression Validator

OR

Change the download file name

Struts 2 Download - How to configure the file name dynamically?

like image 44
Alireza Fattahi Avatar answered Oct 17 '22 22:10

Alireza Fattahi


  • ${} - Standard JSP EL notation.
  • #{} - Standard UEL notation; never used it, may work.
  • %{} - OGNL expression notation.

JSP EL notation works because there's a request wrapper that will defer to the value stack for lookups first, then fall back to the normal JSP evaluation if there's no value found on the value stack.

OGNL expression notation is valid only within S2 tags. IMO it should be used whenever you are evaluating an OGNL expression, although it is quite often optional. When it is optional is somewhat of a crap shoot, however. It often is, buuuut not always. Best to use it and be explicit and communicative.

You may be asking about # variables, like #session etc. # is used to resolve a value on the value stack that's in the "map" portion. I view the value stack as a combination stack and scope: if an object has been pushed on the stack, you don't need the #. If a value has been created, you need the #.

For example, if you use <s:set> to create a variable, you must access it using a # prefix, like:

<s:set var="foo" value="'plugh'"/>
<s:property value="#foo"/>
like image 155
Dave Newton Avatar answered Oct 18 '22 00:10

Dave Newton