Spring mobile documentation suggest adding the following configuration:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<beans:bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
to pass the current Device object as an argument to @Controller
methods.
Yet, one can use:
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
}
and bypass <mvc:annotation-driven>
configuration.
Then, how to add a DeviceWebArgumentResolver
programmatically?
Solution (Luciano):
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addArgumentResolvers(
List<HandlerMethodArgumentResolver> argumentResolvers) {
// Adding Spring mobile argument resolvers
argumentResolvers.add(
new ServletWebArgumentResolverAdapter(
new DeviceWebArgumentResolver()));
argumentResolvers.add(
new ServletWebArgumentResolverAdapter(
new SitePreferenceWebArgumentResolver()));
}
// ...
}
WebMvcConfigurerAdapter
contains a method named addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)
That is the method you need to implement, but first you need to convert a WebArgumentResolver
(in this case, DeviceWebArgumentResolver
) into a HandlerMethodArgumentResolver
. For that, there exists a class named ServletWebArgumentResolverAdapter
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With