File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes display text field without value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "display text field without value" Watch "display text field without value" New topic

display text field without value

James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
I have a question for ActionForm
Say I have an ActionForm with one field named "address", like this:

public class AddressForm extends ActionForm{
private String address;
//setter and getter methods here
public void set...
public String get...

Initially, "address" field is null. When I use this form in JSP to render a text field like this:
<html:text name="addressForm" property="address"/>

It actually displays html page with a ZERO length empty string like this:
<input type="text" name="address" value="">

this caused me big trouble. I don't want "value" exist because if it does I will get empty address instead null when I submit the form. I actually want to have null address, like this:
<input type="text" name="address">
Is there a way to force this in Struts? Or I have to do it myself?
[ October 26, 2006: Message edited by: James Quinton ]
Prasanth Pillai
Ranch Hand

Joined: Oct 16, 2006
Posts: 52
I think you can not aviod the generation of value field. One thing you may do it, you can assign a "null" to the value to the address field by calling the setter method. This you may do it in the begining of Action class.

Like in the begining of execute method,

if ( yourActionForm.address.equals("") || (yourActionForm.address.trim().length == 0 ) {

yourActionForm.setAddress(null);//whatever you want to set


or you may write a js funtion inside the JSP & call that fuction onsubmit.

hope this helps

Prasanth Pillai
Ranch Hand

Joined: Oct 16, 2006
Posts: 52
also you can try with initializing your address variable to NULL in actionForm.

String address = null;
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If this were my application, I'd just put logic on the back end to substitute null for an empty string before updating the database.

However, if you really want to handle it on the front end, I'd suggest using the regular HTML <input> tag instead of the Struts <hmtl:text> tag. That way you can manipulate it however you want. As long as the name attribute is the same as the corresponding property in your ActionForm, Struts will still populate it. The down side of doing it this way is that if there is a validation error that causes the page to be redisplayed, the data entered by the user will not show in the fields.

Consultant, Sima Solutions
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
thank you all for you reply. I decide to give up and do it my way.

I tried
String address=null;
It's not working I think without explicitly being assigned a value, String is defaulted to null.

I have to reply on struts html tag because my form has lots of validations. I cannot afford to lose those values.

I am going to implement the logic in the backend in my own way so that I can manipulate the data.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
There is an init param that can be set in web.xml for ActionServlet that will cause empty values to be converted to null values in your ActionForm. The name of this parameter is convertNull (it used to be named convertHack). My understanding is that this was the default behavior of Struts 1.0. Keep in mind that this will affect all your actions, so it would not be a good idea to change this the day before releasing a large application to production.

- Brent
I agree. Here's the link:
subject: display text field without value
It's not a secret anymore!