[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1680
  • Last Modified:

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
0
moussehead
Asked:
moussehead
  • 2
  • 2
1 Solution
 
rrzCommented:
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>
0
 
mousseheadAuthor Commented:
Sorry, that didn't work.  Seems like c:set just passes through to the jsp:setProperty method.  Any other ideas?
0
 
rrzCommented:
It works for my test case. Please  try the following.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="testBean" class="rrz.TestBean" scope="session"/>
<c:forEach var="entry" items="${param}">
                 <c:set target="${testBean}" property="${entry.key}" value="${entry.value}"/>
</c:forEach>
<html><body><form>
                  <input type="text" name="one"/>
                  <input type="text" name="two"/>
                  <input type="submit"/>
</form>
one = ${testBean.one}<br/>
two = ${testBean.two}
</body></html>
//////////////////////////////////////////////////////////
package rrz;
public   class TestBean{
   private String one = null;
   private String two = null;
   public void setOne(String one){this.one = one;}
   public void setTwo(String two){this.two = two;}
   public String getOne(){ return one;}
   public String getTwo(){ return two;}
}

Open in new window

0
 
mousseheadAuthor Commented:
That did the trick, just not in JSTL 1.0, which I'm stuck with for the moment.  Thanks for your help!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now