This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a form to pass non-string objects to a bean

 
Alan Faulkner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey folks,

Wondered if anyone could help me out. I'm quite new to Java EE and I'm struggling a bit with some form processing in a Spring web app I'm building.

Basically the form is taking several pieces of textual input and mapping these onto fields in the model bean as expected. No biggie.

But one of the fields in the bean is not of type String, its of type Date. While text entry is certainly not the ideal format for entering a date, thats a whole other issue
I'm gonna worry about later.

What I want to know is, how can I get my JSP to take form input and package it into a date object? Or any object for that matter; and allow this object
to be passed to a field in the model class.

Apologies if my explanation is a bit sparse, at the moment I'm not sure what stuff I'm dealing with is Spring and which is plain Java EE.

Anyway, I've attached the relevant code snippets below:

JSP




Controller



Model Class




Servlet Mapping XML



 
Alan Faulkner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies.
 
Ashutosh M Kulkarni
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For one thing, you could make the user enter date in a specific format e.g. MM/DD/YYYY and then use a SimpleDateFormat object to convert the String to a Date.

SOURCE HERE.

This however might bring up another problem as the author of the above link was facing.
If user enters say a date as 12/22/08, it will be formatted into 12/22/0008. The solution to this problem lies in the Javadocs as stated below.
(Also, you can put client-side verification for DateFormat you desire.)
For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the SimpleDateFormat instance is created. For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat instance created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964.

SOURCE HERE.


If there is a better way, I am sorry I am not aware about it.

Thanks,
Ashutosh.
 
Alan Faulkner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for your reply Ashutosh, I'll try that out tonight and let you know how I get on.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic