File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes IF condition in Struts 2.1.6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "IF condition in Struts 2.1.6" Watch "IF condition in Struts 2.1.6" New topic

IF condition in Struts 2.1.6

naveen putrevu

Joined: Sep 09, 2007
Posts: 22

Consider my scenario:
In the JSP, I have a field named 'gender' with male, female as values. In the DB, I want to save it as 'M' or 'F' if the selected name is male or female respectively.

When I retrieve this value from DB, I want to check in my JSP; if the value is 'M', the select box in JSP should show the value as 'Male'. 'Female' otherwise. So, I have written the following if condition in JSP.

<s:if test="%{gender == 'Male'}">
<bsp:select name="gender" list="{'Male','Female'}" value="%{gender}" cssClass="formField"/>
<bsp:select name="gender" list="{'Male','Female'}" value="%{gender}" cssClass="formField"/>

I am unable retrieve it properly: meaning, the JSP always shows 'Male' even though the value in the DB is 'Female'. I would like to know what is the correct way to represent the IF condition.

[I am using Struts 2.1.6]
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

why not you compare with M or F in if statements?

Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

If you are using struts, then why don't you try the struts select tag with a map of properties like this

list="{'M':'Male','F':'Female'}" value="gender"

Using this you'll not need any condition or anything. I think you'll iterate over a collection or have a single object of User or whatever class in your action so the gender property will automatically be taken from the value stack...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: IF condition in Struts 2.1.6
It's not a secret anymore!