This chapter documents a set of Hades extensions that enable Hades usage in a variety of contexts. Currently most of the integration is targeted towards Spring MVC.
Given you are developing a Spring MVC web applications you typically have to resolve domain class ids from URLs. By default it's your task to transform that request parameter or URL part into the domain class to hand it layers below then or execute business logic on the entities directly. This should look something like this:
@Controller
@RequestMapping("/users")
public class UserController {
private final UserDao userDao;
public UserController(UserDao userDao) {
this.userDao = userDao;
}
@RequestMapping("/{id}")
public String showUserForm(@PathVariable("id") Long id, Model model) {
// Do null check for id
User user = userDao.readByPrimaryKey(id);
// Do null check for user
// Populate model
return "user";
}
}First you pretty much have to declare a repository dependency for
each controller to lookup the entity managed by the controller or DAO
respectively. Beyond that looking up the entity is boilerplate as well
as it's always a readByPrimaryKey(…) call.
Fortunately Spring provides means to register custom converting
components that allow conversion between a String value to an arbitrary
type.
For versions up to Spring 3.0 simple Java
PropertyEditors had to be used. Thus,
Hades offers a
GenericDaoPropertyEditorRegistrar, that will
look up all Hades repositories registered in the
ApplicationContext and register a
custom PropertyEditor for the managed
domain class
<bean class="….web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="….web.bind.support.ConfigurableWebBindingInitializer"> <property name="propertyEditorRegistrars"> <bean class="org.synyx.hades.extensions.beans.GenericDaoPropertyEditorRegistrar" /> </property> </bean> </property> </bean>
If you have configured Spring MVC like this you can turn your controller into the following that reduces a lot of the clutter and boilerplate.
@Controller
@RequestMapping("/users")
public class UserController {
@RequestMapping("/{id}")
public String showUserForm(@PathVariable("id") User user, Model model) {
// Do null check for user
// Populate model
return "userForm";
}
}As of Spring 3.0 the
PropertyEditor support is superseeded
by a new conversion infrstructure that leaves all the drawbacks of
PropertyEditors behind and uses a
stateless X to Y conversion approach. Hades extensions now ship with a
GenericDaoConverter that pretty much mimics the
behaviour of GenericDaoPropertyEditorRegistrar.
To register the converter you have to declare
ConversionServiceFactoryBean, register the
converter and tell the Spring MVC namespace to use the configured
conversion service:
<mvc:annotation-driven conversion-service="conversionService" /> <bean id="conversionService" class="….context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="org.synyx.hades.extensions.conversion.GenericDaoConverter"> <constructor-arg ref="conversionService" /> </bean> </list> </property> </bean>