Link to home
Start Free TrialLog in
Avatar of moussehead
moussehead

asked on

How to set a bean property to empty string using jsp/jstl?

I have a jsp that uses <jsp:setProperty name="beanname" property="*"/> to map submitted form values to a bean.  This is great unless a user wants to blank out a value (say, they put an address part in the wrong field and go back to correct.)  According to the documentation, jsp:setPropoerty will ignore any empty strings passed to it and leave the existing bean value.

So, I tried using JSTL tags like so:

<c:forEach var="parm" items="${param}">
  <c:set target="beanname" property="${parm.name}" value="${parm.value}"/>
</c:forEach>

However, this behaves exactly the same: values that have input in the form are set, but those that are empty retain their former values.  

Anyone know how I can set the values to empty strings once they've been set?  

Thanks,
David
Avatar of rrz
rrz
Flag of United States of America image

Your idea is good. It just needs two adjustments.  
1) put ${} around beanname
2) use parm.key instead of using  parm.name
<c:forEach var="parm" items="${param}">
                 <c:set target="${beanname}" property="${parm.key}" value="${parm.value}"/>
</c:forEach>
Avatar of moussehead
moussehead

ASKER

Sorry, that didn't work.  Seems like c:set just passes through to the jsp:setProperty method.  Any other ideas?
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did the trick, just not in JSTL 1.0, which I'm stuck with for the moment.  Thanks for your help!