Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IF condition in Struts 2.1.6

 
naveen putrevu
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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"/>
</s:if>
<s:else>
<bsp:select name="gender" list="{'Male','Female'}" value="%{gender}" cssClass="formField"/>
</s:else>

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
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not you compare with M or F in if statements?
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic