Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

JSP property value

in Struts 2  JSP I have this code..

<s:property value="#session['selectedItems']" />  // This prints:  [1]
<s:property value="#session['selectedItems'].contains('1')"/> // This prints: false

 I dont understand why it prints  false . It should print true as list contains "1"
Avatar of for_yan
for_yan
Flag of United States of America image



Session in Struts coresponds to Map object

(see

http://www.roseindia.net/struts/struts2/struts2ajax/struts2-session.shtml)

Map has method method containsValue , then probably session.containsValue("1")
will return true, as "1" is in fact value corresponding to one of the keys


Well,maybe I'm wrong, I now see,  you mean that
session['selectedItems'] is in itself the List
Avatar of Mick Barry
Does the list contain the string 1, or the value 1?
You are checking if it contains a string "1"
Avatar of cofactor
cofactor

ASKER

>>>Does the list contain the string 1, or the value 1?

String 1  

Because  the list has been populated  with the value  from the  checkbox field  and checkbox field has value = "1".

So, the list contains all  String values .

I dont understand where things are wrong ?  how do I troubleshoot further ? I see the prints in the list correctly but the match returning false !
how do I troubleshoot further ?
> <s:property value="#session['selectedItems'].contains('1')"/> // This prints: false

could be looking for character '1', instead of string "1"
try changing the quotes
>>>try changing the quotes

I tried this...

<s:property value="#session['selectedItems'].contains("1")"/>

but this is a syntax error in jsp...how do I correct it ?
perhaps you should escape quotes like they do it in Java
(don't know if that would work in JSP):
<s:property value="#session['selectedItems'].contains(\"1\")"/>
I think the pblm is ...
>>><s:property value="#session['selectedItems'].contains('1')"/>
this will return boolen values so it may not assign !!
But it still prints 'false" - so it can print boolean value?
>>><s:property value="#session['selectedItems'].contains(\"1\")"/>

Bingo.  This works fine  :)

syntax looks bad but this just works fine .

However, I'm worried if I  would like to put a place holder variable  say x  instead of  constant "1"  , how do I do that ?

Will this be a correct syntax in that case ?
<s:property value="#session['selectedItems'].contains(\"+x+\")"/>  // x is a place holder with value="1"

No if it is avraiable you should have two quotes:
<s:property value="#session['selectedItems'].contains(\""+x+"\")"/>  // x is a place holder with value="1"


...the specification for the contains(Object key) method says: "returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)).
So, your code tests if the map contains the key '1', not the value '1'.
Or maybe simply:
<s:property value="#session['selectedItems'].contains(x)"/>  // x is a place holder with value="1"

or maybe like in JSP context I guess they use % or something
you'd just use this wouldn't you

<s:property value="#session['selectedItems'].contains(x)"/>  // x is a place holder with value="1"

>>><s:property value="#session['selectedItems'].contains(x)"/>  // x is a place holder with value="1"

Awesome. Yes, this is fine.

I  have one more doubt . I  have seen a syntax  in JSP  with Struts 2  to retrieve values  using symbol as  %{variable}  . I  just dont understand how is it different than retrieving values using  "#"  like the example I posted. Why there is two separate symbols to retrieve values ? This is confusing.

So,could you please tell when should I use  %{variable}  to retrieve values and when should I use  # symbol  to retrieve values . I'm confused at this part.

N.B  POINTS increased to 400



I guess % notation comes from JSP
and # is from struts equivalent to something like
 ActionContext.getContext ();
still not clear.
>>> guess % notation comes from JSP

Does that mean , If I would like to access a variable defined in JSP page , then I should use %{jsp_variable}

>>and # is from struts equivalent to something like

Does that mean , If I would like to access a session variable set  in the Action class , then I should use # notation in the jsp page  ?

Could you please clarify further ? I'm not clear when to use these notations .
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
partially ok