For this project we will only use a WebApplicationContext so the only thing we need to do is configure the Spring DispatcherServlet.
1
Step-By-Step
2
sbs
3
org.springframework.web.servlet.DispatcherServlet
4
1
5
6
sbs
7
*.do
8
9
index.jsp
As you see we mapped all the *.do to the dispatcher servlet. Problem with the servlet spec is that the welcome page has to be a physical file. So the index.jsp only does a redirect to index.do. The Servlet is named sbs. As with the Spring convention we then need to create sbs-servlet.xml to get the context loaded. For this example I use the default BeanNameUrlHandlerMapping, which resolve an URI to a Controller.
We also need something to resolve our views (a ViewResolver) so I configured an InternalResourceViewResolver, which looks up jsp in the WEB-INF/jsp directory.
[sourcode language='xml']
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd”>
[/code]Well that is it, configuration wise. We have a controller, with a form and a validator, we have 2 views (entry and success).
The controller isn't that big of a deal, it extends simpleformcontroller and only implements the initBinder method. For binding to other objects then Strings or primitives we need a PropertyEditor. Luckily for us Spring comes shipped with some PropertyEditors. For this example we will use the CustomDatePropertyEditor. The PropertyEditor will convert a String to a Date and vice-versa.
1
public class DateEntryController extends SimpleFormController {
2
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
3
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
4
CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
5
binder.registerCustomEditor(Date.class, editor);
6
}
7
}
The initBinder method as mentioned here, registers a CustomDateEditor which takes a text in the format (dd-MM-yyyy) and transforms that into a valid date.
the line 'binder.registerCustomEditor(Date.class, editor);' tells the binder to register this propertyeditor for all the Date properties on the command object. If there is a need for multiple property editors for the same type you can also specify the name of the property on the command object.
The Validator is a simple one (just to generate some errors and show that valid dates are translated back into strings).
01
public class DateEntryValidator implements Validator {
02
03
public boolean supports(Class clazz) {
04
return DateEntryFormObject.class.isAssignableFrom(clazz);
05
}
06
07
public void validate(Object target, Errors errors) {
08
DateEntryFormObject commandObject = (DateEntryFormObject) target;
09
Date startDate = commandObject.getStartDate();
10
Date endDate = commandObject.getEndDate();
11
12
if (startDate == null) {
13
errors.rejectValue("startDate", "required", "Starting Date is required.");
14
}
15
16
if (startDate != null && endDate != null && endDate.before(startDate)) {
17
errors.rejectValue("endDate", "notbefore.startdate", "End date cannot be before start date.");
18
}
19
}
20
}
Well that's all there is. The only remaining things are the 2 jsps and the command object.
01
public class DateEntryFormObject implements Serializable {
02
03
private Date startDate;
04
private Date endDate;
05
06
public Date getEndDate() {
07
return endDate;
08
}
09
10
public void setEndDate(Date endDate) {
11
this.endDate = endDate;
12
}
13
14
public Date getStartDate() {
15
return startDate;
16
}
17
18
public void setStartDate(Date startDate) {
19
this.startDate = startDate;
20
}
21
}
entry.jsp
01
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form">
02
Enter dates in format (dd-MM-yyyy) | ||
Start Date: | ||
End Date: | ||
01
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form">
02
Dates entered | |
Start Date: | |
End Date: |
There is just one important thing to remember PropertyEditors work only for the command object, not for reference data, or objects stored somewhere else. For those to format correctly you will need the good ol' fmt tags.
No comments:
Post a Comment