1. Dispatcher Servlet configuration
<bean id="viewResolver"
class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
p:basename="views" />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
p:definitions="/WEB-INF/tiles-defs.xml" />
<flow:flow-builder-services id="flowBuilderServices"
view-factory-creator="viewFactoryCreator" conversion-service="conversionService" />
2. Write Convert Class
java.util.Calendar to String Custom Converter / String to java.util.Calendar Custom Converter
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.springframework.binding.convert.converters.TwoWayConverter;
public class CalendarStringTwoWayConverter implements TwoWayConverter {
@Override
public Class getSourceClass() {
return Calendar.class;
}
@Override
public Class getTargetClass() {
return String.class;
}
@Override
public Object convertSourceToTargetClass(Object source, Class targetClass)
throws Exception {
String date = null;
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
if(null!=source){
date= dateFormat.format(((Calendar) source).getTime()).toString();
}
return date;
}
@Override
public Object convertTargetToSourceClass(Object target, Class sourceClass)
throws Exception {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
try {
cal.setTime(sdf.parse((String) target));
} catch (ParseException e) {
e.printStackTrace();
}
return cal;
}
}
3. Register Converter
import org.springframework.binding.convert.service.DefaultConversionService;
import org.springframework.stereotype.Component;
@Component("conversionService")
public class ApplicationConversionService extends DefaultConversionService {
@Override
protected void addDefaultConverters() {
super.addDefaultConverters();
addDefaultAliases();
addConverter("calenderStringTwoWayConverter", new CalendarStringTwoWayConverter());
addConverter("enumStringTwoWayConverter", new EnumStringTwoWayConverter());
addConverter("timeStringTwoWayConverter", new TimeStringTwoWayConverter());
}
}
Use in flow registry : Bind properties with converter
<view-state id="review" view="review" model="reviewModel">
<binder>
<binding property="state" converter="enumStringTwoWayConverter"/>
<binding property="time" converter="timeStringTwoWayConverter"/>
<binding property="date" converter="calenderStringTwoWayConverter"/>
</binder>
</view-state>
This way of using named converters has been discontinued from Spring 3.0. In Spring3.0 the converters are handled in a different way.
4. Use in JSP
There is nothing special in jsp.
<form:form name="myform" modelAttribute="reviewModel">
<form:input type="text" id="date" path="date" value="${date}"/>
</form:form>