• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1699
  • 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}"/>

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?  

  • 2
  • 2
1 Solution
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}"/>
mousseheadAuthor Commented:
Sorry, that didn't work.  Seems like c:set just passes through to the jsp:setProperty method.  Any other ideas?
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}"/>
                  <input type="text" name="one"/>
                  <input type="text" name="two"/>
                  <input type="submit"/>
one = ${testBean.one}<br/>
two = ${testBean.two}
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

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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