views:

94

answers:

3

Should I declare an attribute in a javabean that holds a date value a user types in on an HTML form as a String or Date?

I feel I should declare as a Date, however, since I do server validation on all form data, if the date doesn't validate, when I pass the form bean back to the jsp view for correcting, I lose the date value that the user tried to type in.

If I declare as a String, if the date doesn't validate, I'm able to set the string value in the bean and pass the bean back to the view and the user can see what they incorrectly typed.

But with a String declaration for Date inputs I forsee problems down the road with my DAO. I want to be able to use a DAO utility which generates a prepare statement using setObject.

In my html form I request dates to be mm/dd/yyyy and in DAO i'm using Oracle Date. I can not use hibernate or such, since this is a corporate intranet.

What is the best practice "pattern" I should be following??

A: 

Just store both. It comes in as a String, so save that somewhere. Then validate it. If it fails then pass the original String back to the jsp page.

TheLQ
Assuming he's using some framework for web data binding and not writing raw servlets, that'd be a pretty advanced customization to the binder.
Affe
A: 

Using a javascript mask to not let an unparseable date hit the server in the first place is the most common pattern I've seen for this. You can also find lots of date pickers in open source javascript libraries. If someone knows enough to turn off javascript or otherwise defeat your client side validation, they can probably deal with not getting their unparseable date strings back in the error view.

Affe
that would work. so then are you advocating declaring this attribute in my bean as a Date?
jeff
I would, yes. Date is your internal representation. I would try to leave the code that knows about differences between view representation (a String) and internal representation inside the framework as much as possible.
Affe
+1  A: 

Just store it as Date in the model. You can make use of conditional operator in EL to display the submitted value instead of the model value whenever the model value is absent.

<input name="date" value="${empty bean.date ? fn:escapeXml(param.date) : bean.date}" />

The JSTL fn:escapeXml() is just there to avoid XSS attacks.

To get a step further, you could add an extra layer with a Map<String, Object> params which contains either the submitted value or the final/validated value depending on the validation outcome.

<input name="date" value="${fn:escapeXml(bean.params.date)}" />
BalusC
@BalusC: Perfect!! Remeber me? (I work for the company with the outdated Sun One Server). So I can't Use EL. Here is my hack: Input: <input name="date" value="<c:if test="${empty commitmentItem.need_date}"><c:out value="${param.need_date}" escapeXml='false' /></c:if><c:if test="${not empty commitmentItem.need_date}"><fmt:formatDate type="date" pattern="MM/dd/yyyy" value="${commitmentItem.need_date}" /></c:if>" />
jeff
You're welcome.
BalusC