?
Solved

JSP  property value

Posted on 2011-02-19
19
Medium Priority
?
516 Views
Last Modified: 2012-05-11
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
Comment
Question by:cofactor
  • 8
  • 6
  • 3
  • +2
19 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34935838


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
 
LVL 47

Expert Comment

by:for_yan
ID: 34935849
Well,maybe I'm wrong, I now see,  you mean that
session['selectedItems'] is in itself the List
0
 
LVL 92

Expert Comment

by:objects
ID: 34935869
Does the list contain the string 1, or the value 1?
You are checking if it contains a string "1"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cofactor
ID: 34935960
>>>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
 
LVL 92

Expert Comment

by:objects
ID: 34936052
> <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
 

Author Comment

by:cofactor
ID: 34936085
>>>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
 
LVL 47

Expert Comment

by:for_yan
ID: 34936093
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
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 34936107
I think the pblm is ...
>>><s:property value="#session['selectedItems'].contains('1')"/>
this will return boolen values so it may not assign !!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34936116
But it still prints 'false" - so it can print boolean value?
0
 

Author Comment

by:cofactor
ID: 34936159
>>><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
 
LVL 47

Expert Comment

by:for_yan
ID: 34936164
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
 
LVL 9

Expert Comment

by:ioanton
ID: 34936166
...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
 
LVL 47

Expert Comment

by:for_yan
ID: 34936177
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
 
LVL 92

Expert Comment

by:objects
ID: 34936179
you'd just use this wouldn't you

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

0
 

Author Comment

by:cofactor
ID: 34936216
>>><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
 
LVL 47

Expert Comment

by:for_yan
ID: 34936256
I guess % notation comes from JSP
and # is from struts equivalent to something like
 ActionContext.getContext ();
0
 

Author Comment

by:cofactor
ID: 34936681
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
 
LVL 47

Accepted Solution

by:
for_yan earned 1200 total points
ID: 34936704


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
 

Author Closing Comment

by:cofactor
ID: 35033829
partially ok
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question