Solved

JSP  property value

Posted on 2011-02-19
19
504 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
Comment Utility


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
Comment Utility
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
Comment Utility
Does the list contain the string 1, or the value 1?
You are checking if it contains a string "1"
0
 

Author Comment

by:cofactor
Comment Utility
>>>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
Comment Utility
> <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
Comment Utility
>>>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
Comment Utility
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
Comment Utility
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
Comment Utility
But it still prints 'false" - so it can print boolean value?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

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

Author Comment

by:cofactor
Comment Utility
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 400 total points
Comment Utility


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
Comment Utility
partially ok
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now