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

Posted on 2008-11-11
Last Modified: 2013-11-24
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="${}" 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?  

Question by:moussehead
    LVL 27

    Expert Comment

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

    Author Comment

    Sorry, that didn't work.  Seems like c:set just passes through to the jsp:setProperty method.  Any other ideas?
    LVL 27

    Accepted Solution

    It works for my test case. Please  try the following.
    <%@ taglib uri="" 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 = ${}<br/>
    two = ${testBean.two}
    package rrz;
    public   class TestBean{
       private String one = null;
       private String two = null;
       public void setOne(String one){ = one;}
       public void setTwo(String two){this.two = two;}
       public String getOne(){ return one;}
       public String getTwo(){ return two;}

    Open in new window


    Author Closing Comment

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

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    matchUp  challenge 9 51
    mergeTwo  challenge 13 56
    countPairs challenge 7 39
    Which is the latest version of Apache Tomcatl 3 22
    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now