• 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

Need to make empty text fields not pass null as the value, need them to just not pass anything

 
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a device page where a user inputs a device name and optionally, latitude and longitude for the device. Here is the relevant code from the jsf page:

The values of these latitude and longitude fields are taken and posted to some xml on a web service through this java code (the relevant part):

If I put in a device, let's say named ABBA, and put in values for the lat and lng fields, like 100 and 122, it results in this xml:

Just like it should. However, if I leave the latitude and longitude fields blank it results in this xml:

When I need it to result in this xml:

So I'm not sure if this means I need to make a change in the java code, which seems like it will be more difficult, or a change in the jsf code, which I am hoping because it seems like it would be a simple thing like just adding some attribute.

Thinking about it, it may be simple to do this through the java code, doing something with an if statement and only do device.setProperties(properties); if lat and lng are not null.
I think I probably just solved this so I am about to try it out, but if there is a way to do it through the jsf please let me know. Should I maybe remove immediate=true from those two input fields?
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSF doesn't understand "not pass anything". If you display a form and then post it back, the entire form is considered valid input. There are some subtleties about this process based on how the underlying HTML works, but JSF is going hide that, so it doesn't make any difference.

If a parameter is optional, you'll have to handle that in the backend code. You can either add explicit switches on the form or you can test for a field left blank, like so:

if ( longitude.trim().length() > 0 ) {
properties.put("lng", deviceLng);
}

Note that this means that longitude has to be defined as a text property, so you should also supply an appropriate validator.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic