• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 526
  • Last Modified:

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"
0
cofactor
Asked:
cofactor
  • 8
  • 6
  • 3
  • +2
1 Solution
 
for_yanCommented:


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


0
 
for_yanCommented:
Well,maybe I'm wrong, I now see,  you mean that
session['selectedItems'] is in itself the List
0
 
objectsCommented:
Does the list contain the string 1, or the value 1?
You are checking if it contains a string "1"
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
cofactorAuthor Commented:
>>>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 ?
0
 
objectsCommented:
> <s:property value="#session['selectedItems'].contains('1')"/> // This prints: false

could be looking for character '1', instead of string "1"
try changing the quotes
0
 
cofactorAuthor Commented:
>>>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 ?
0
 
for_yanCommented:
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\")"/>
0
 
Sathish David Kumar NArchitectCommented:
I think the pblm is ...
>>><s:property value="#session['selectedItems'].contains('1')"/>
this will return boolen values so it may not assign !!
0
 
for_yanCommented:
But it still prints 'false" - so it can print boolean value?
0
 
cofactorAuthor Commented:
>>><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"

0
 
for_yanCommented:
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"


0
 
ioantonCommented:
...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'.
0
 
for_yanCommented:
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
0
 
objectsCommented:
you'd just use this wouldn't you

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

0
 
cofactorAuthor Commented:
>>><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



0
 
for_yanCommented:
I guess % notation comes from JSP
and # is from struts equivalent to something like
 ActionContext.getContext ();
0
 
cofactorAuthor Commented:
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 .
0
 
for_yanCommented:


Well, that is my undertsanding. # symbol is a part of OGNL
(http://struts.apache.org/2.0.14/docs/ognl.html)
and it introducses this hash notation.

% exist in JSP even if you don't have any Struts
0
 
cofactorAuthor Commented:
partially ok
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 8
  • 6
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now