Petr Tomasek wrote:
> Hi,
>
> the <jsp:setProperty /> tag sets the beans properties using bean setter
> method. But setting an empty string doesn't help.
>
> Imagine you have a form with one text field:
>
> <form action="this.jsp" method="post">
> <input type="text" name="foo" value="<+ACU-= myBean.getText() +ACU->">
> </form>
>
> And the myBean.text property is set in every page load through:
>
> <jsp:useBean id="myBean" class="TestBean" scope="request" />
> <jsp:setProperty name="myBean" property="+ACo-" />
>
> If you fill in the form and submit it, it will contain the value you have
> entered. But if you submit empty string, the value will not be changed. It
> will be ignored.
>
> This is very bad behaviour, because user cannot delete values, they can only
> change them to another values.
>
> Is it possible to instruct the bean setter not to ignore empty strings? What
> would you recommend to do here?
>
> I am using Tomcat 4.0.3 bound to IIS on Windows 2000.
The behavior you see for empty strings is exactly what the spec says.
The rationale, I believe, is that the behavior for an empty text field
should be the same as for an unchecked checkbox (or no selection in
a selection list), where no parameter is sent with the request at all.
Not sure this was a good decision, though ;-)
Anyway, if you create a new bean for each request (as in your example),
you just have to be aware of this and initialize the properties to
the "unset" value you want (e.g. an empty string for a String property).
Thus, if the setter method is not called by <jsp:setProperty>, your
getter method still returns the default value you want.
If you place the bean in the session scope and want the user to be
able to change the values with a new request (including removing the
value), you can add a reset property that resets all properties:
package com.mycomp;
public class MyBean +AHs-
private String foo = "";
public void setFoo(String foo) +AHs-
this.foo = foo;
+AH0-
public String getFoo() +AHs-
return foo;
+AH0-
public void setReset(String ignore) +AHs-
foo = "";
+AH0-
+AH0-
You can then use it like this:
<jsp:useBean id="myBean" class="com.mycomp.MyBean" scope="session" />
<jsp:setProperty name="myBean" property="reset" value="" />
<jsp:setProperty name="myBean" property="+ACo-" />
I hope this helps,
Hans
--
Hans Bergsten hans+AEA-gefionsoftware.com
Gefion Software http://www.gefionsoftware.com
JavaServer Pages http://TheJSPBook.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com