aspose file tools*
The moose likes Struts and the fly likes Struts2: Compare request parameter not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2: Compare request parameter not working" Watch "Struts2: Compare request parameter not working" New topic
Author

Struts2: Compare request parameter not working

Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
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?">


Nick
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Is this a typo? If not, not enough info to help yet.
Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
What info do you need?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, I need to know if it's a typo.
Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
It is correct, and that is how I am using the s:if tag in my JSP.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Okay, well, fix the typo first, and see if it works then.
Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
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

Joined: Sep 29, 2008
Posts: 12617

In your actual code is there anything *in* the tag body?
Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
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

Joined: Aug 03, 2008
Posts: 9305
    
  17

Why are you using single quotes around false??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
because double quotes will not work. If I remove quotes, that doesn't work either.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 10, 2008
Posts: 78
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 10, 2008
Posts: 78
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

Joined: Nov 10, 2008
Posts: 78
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts2: Compare request parameter not working