http parameter casting

J

Justin

This is a web form design discussion. When handling input from a web
form I usually have a "form" object that handles getting the parameters
from the request validating etc. I want to store what the user enters
in variables of the appropriate type. (i.e. if they enter a quantity it
comes through the request as a String but should be stored as an int).
Now let's say the user is prompted to enter a date. The "form" class
attempts to parse the data but fails. When the form renders with the
error message explaining the required format for dates I want to
populate the input with the users previous entry. There in lies the
problem. I've lost what they entered when I tried to parse the date
and it failed. That is unless I store all of the user's original
string input in the form object. Another option might be to have a
form manager object that maintains the data as Strings until it's valid
and then kicks out a business object that contains appropriately typed
values. I'm interested in other suggestions and opinions on what
I've listed above.
 
R

Ryan Stewart

Justin said:
This is a web form design discussion. When handling input from a web
form I usually have a "form" object that handles getting the parameters
from the request validating etc. I want to store what the user enters
in variables of the appropriate type. (i.e. if they enter a quantity it
comes through the request as a String but should be stored as an int).
Now let's say the user is prompted to enter a date. The "form" class
attempts to parse the data but fails. When the form renders with the
error message explaining the required format for dates I want to
populate the input with the users previous entry. There in lies the
problem. I've lost what they entered when I tried to parse the date
and it failed. That is unless I store all of the user's original
string input in the form object. Another option might be to have a
form manager object that maintains the data as Strings until it's valid
and then kicks out a business object that contains appropriately typed
values. I'm interested in other suggestions and opinions on what
I've listed above.
Opinion 1: Use Struts
Opinion 2: Use some other framework like Struts
Opinion 3: Use Struts

In technical terms, we call what you're doing "reinventing the wheel". Even if
you use no other part of the Struts framework, its form handling does all this
for you behind the scenes.

If, for some odd reason, this isn't an option available to you, then yes, you
want to retain the original String data entered by the user. If you lose the
String when you try to parse a date, I assume you are not storing the String
first? One way would be to store all the String values in the form, and
transform them only upon request. It may add a tiny bit of overhead, but how
often are you going to request the same thing from the form twice?
 
C

Chris Smith

Justin said:
This is a web form design discussion. When handling input from a web
form I usually have a "form" object that handles getting the parameters
from the request validating etc. I want to store what the user enters
in variables of the appropriate type. (i.e. if they enter a quantity it
comes through the request as a String but should be stored as an int).

That's entirely normal. Contrary to the subject of your post, you can't
accomplish this by casting. You have to use a conversion routine;
preferably a java.text.XXXFormat class.
Now let's say the user is prompted to enter a date. The "form" class
attempts to parse the data but fails. When the form renders with the
error message explaining the required format for dates I want to
populate the input with the users previous entry. There in lies the
problem. I've lost what they entered when I tried to parse the date
and it failed. That is unless I store all of the user's original
string input in the form object.

This isn't exactly true. That information will always be available with
request.getParameter(name) even after you've retrieved it from the
request and tried to parse it. A really simple answer to your problem
would be to just get it from request.getParameter.

Alternatively, yes it is possible to store both the original user input
and the parsed data type in your application. For example, JSF does
this, using strings for a text field's data, but parsing it
automatically using a converter so that it can be provided as a date,
number, etc. to the application. That's generally a little more robust
than relying on request.getParameter, mainly because it isn't as
dependent on the exact details of the way that HTTP and forms work.

Incidentally, why aren't you using JSF or Struts or Spring or something
similar for this task?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Malte

Justin said:
This is a web form design discussion. When handling input from a web
form I usually have a "form" object that handles getting the parameters
from the request validating etc. I want to store what the user enters
in variables of the appropriate type. (i.e. if they enter a quantity it
comes through the request as a String but should be stored as an int).
Now let's say the user is prompted to enter a date. The "form" class
attempts to parse the data but fails. When the form renders with the
error message explaining the required format for dates I want to
populate the input with the users previous entry. There in lies the
problem. I've lost what they entered when I tried to parse the date
and it failed. That is unless I store all of the user's original
string input in the form object. Another option might be to have a
form manager object that maintains the data as Strings until it's valid
and then kicks out a business object that contains appropriately typed
values. I'm interested in other suggestions and opinions on what
I've listed above.

In my current application project I have a need to:

a: get form input from a user
b: validate the input and stick it in Oracle
c: be able to get the data for subsequent editing
d: supply all functions via html and via xml based web services

Therefore I have chosen this model:

Generate HTML output from XML using XSLT. Using this model it is very
easy to:

a: accept form input
b: pass the HttpRequest to a FormChecker class that returns a form
object. This object also has a getAsXML() method that wil render the
form's fields as xml. This xml is the same as the xml that would be
returned from the database.

Thus, I can reuse my XSL stylesheet. If there is data, it will be shown,
if there is not, an input field in the form will just be empty.

If validation fails, all other fields are in the xml, and I have a small
method that will put out an error message pointing to the offending
input object.

Oh yes, I do know about Struts, but the xml requirement (for web
services) is a strong one, and I have no intention of writing two GUI's,
because it is my experience that once the business logic is in place,
the GUI will be changed frequently.
 
R

Ryan Stewart

Malte said:
In my current application project I have a need to:

a: get form input from a user
b: validate the input and stick it in Oracle
c: be able to get the data for subsequent editing
d: supply all functions via html and via xml based web services

Therefore I have chosen this model:

Generate HTML output from XML using XSLT. Using this model it is very easy to:

a: accept form input
b: pass the HttpRequest to a FormChecker class that returns a form object.
This object also has a getAsXML() method that wil render the form's fields as
xml. This xml is the same as the xml that would be returned from the database.

Thus, I can reuse my XSL stylesheet. If there is data, it will be shown, if
there is not, an input field in the form will just be empty.

If validation fails, all other fields are in the xml, and I have a small
method that will put out an error message pointing to the offending input
object.

Oh yes, I do know about Struts, but the xml requirement (for web services) is
a strong one, and I have no intention of writing two GUI's, because it is my
experience that once the business logic is in place, the GUI will be changed
frequently.

I'm interested in this type of approach because I'm on a project in which we may
be doing something very similar in the coming months. What do you mean by
"writing two GUIs"? Are you not using a web application? I don't see where
Struts would interfere with the process you're describing.
 
M

Malte

Ryan said:
I'm interested in this type of approach because I'm on a project in which we may
be doing something very similar in the coming months. What do you mean by
"writing two GUIs"? Are you not using a web application? I don't see where
Struts would interfere with the process you're describing.
Hmm, I meant that we often have two different kinds of client: a browser
or a 'system'. The system needs to get the data as XML (via a Web
Service). The browser needs to get the data wrapped in (x)html. The
common denominator would then be XML, since I do not want to access the
database directly using some GUI related framework (Oracle's ADF, former
BC4J comes to mind). I have not found a good way yet to render xml in a
browser without running it through xsl. It may be possible to have xslt
do the 'Struts markup', I just haven't tried this yet. I tend to play
with one technology at a time ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top