• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2: Compare request parameter not working

 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to compare a request parameter with a string and it is not working. Here is what I have done so far:

The request parameter is isServer=false.
In the JSP, I have and it is not working.

Any idea what I am missing?">
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a typo? If not, not enough info to help yet.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What info do you need?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I need to know if it's a typo.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is correct, and that is how I am using the s:if tag in my JSP.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, well, fix the typo first, and see if it works then.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh if you meant the quotes, then this is what I am using, and it doesn't work:



Sorry, I didn't notice that earlier. If there is any other typo you are refering to then please let me know.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your actual code is there anything *in* the tag body?
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am trying to print some test text for output to see if it actuall works.

Eg:


Strange thing is that I am using %{#parameters} object in other JSP and it works. Still puzzled.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using single quotes around false??
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because double quotes will not work. If I remove quotes, that doesn't work either.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AFAIK the result of getting a parameter like that will be an array, since a parameter can have multiple values.

As the developer, you have the power to check stuff like this yourself:The more important question is why you'd want to do this, since there's a built-in mechanism to make things like this much, much cleaner, and allow you to use the parameter as a real boolean.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry David, I've been travelling over the weekend. If I try to output the class the output is class [Ljava.lang.String;

I did not understand when you mentioned that I can use parameter as real boolean. What did you meant by that? Did you mean that I should have a getter and setter for the request parameter in the Action class?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if you wanted to do a string compare, what would you need to do?

Ultimately, yes, I don't see any good reason to be comparing a request parameter directly in the JSP, and for something that's boolean, even less reason to be doing it as a String since the request will have already passed through the normal S2 interceptors, which will do the type conversion for you.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the pointer David. I eventually added a getter in my action class which is as shown below:


in JSP I do the following:

The false block never gets executed even when the request parameter is false. Not sure what I missed. Any idea?

This does look much cleaner.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. Figured that out. I now have a getter and setter in the action class, with a boolean property which has the same name as the parameter I was using. Now I dont have to do and getParameterAsString.

This is good.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic