Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • ${} - 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"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...