Solved

JSP  property value

Posted on 2011-02-19
19
506 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 400 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Arrays.asList  VS  ArrayList 4 54
tomcat administrtor 12 46
login form jsp example 2 24
What's wrong with this code? 4 22
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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