• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

display text field without value

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Prasanth Pillai
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also you can try with initializing your address variable to NULL in actionForm.

String address = null;
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
James Quinton
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic