I have a Javabean with a date_observed field. Now I am trying to create a form where a user can search for entries between a start and end date.
Should I create another Javabean that extends this bean to have a start and end date field so that I can populate these field from request parameter?
I’d like to cleanly pass a bean to my Dao for SQL string generation and also have a way to do form validation if they enter incorrect date format.
Typically I would do
public void processDate_observed(HttpServletRequest request, Comment comment) {
String _date = FormUtil.getFieldValue(request, FIELD_DATE_OBSERVED);
if (!"".equals(_date) && _date != null) {
try {
Date date = DateUtil.parse(_date);
com.setDate_observed(date);
} catch (ParseException e) {
setError(FIELD_DATE_OBSERVED, e.getMessage());
}
}
}
But my Comment Javabean does not have fields for start_date and end_date
And for dao.search(comment)
public List<Evaluatee> search(Comment comment) {
SQL = ".... where date_observed > ? AND date_observed <= ?
ps.setDate(1, comment.EXTENDED FIELD???)
ps.setDate(2, comment.EXTENDED FIELD???)
...
}
What is the best practice here? Creat a whole new Javabean, extend my original bean or pass along the two date fields to Dao? But then how do you pass form validation back to form if I don’t have the dates in a bean?
Is a good practice that in your DAO class you have a serch method with a start and end date as parameters.
It’s not necessary that the DAOs needs to have a Comment object as an argument, the rule is that for each table of the data base need to be a class with the same fields as in the table.