spring mobile是个啥???它具备以下功能
Features
1.A Device resolver abstraction for server-side detection of mobile and tablet devices
2.Site preference management that allows the user to indicate if he or she prefers a "normal", "mobile", or "tablet" experience
3.A site switcher capable of switching the user to the most appropriate site, either mobile, tablet, or normal, based on his or her device and optionally indicated site preference
4.Device aware view management for organizing and managing different views for specific devices
快速入门(实现上面的第1点功能)
1.WebMvcConfigurerAdapter
- /**
- * User: xiejx618
- * Date: 13-11-25
- * Time: 下午1:53
- */
- @EnableWebMvc
- @Configuration
- @ComponentScan(basePackages={"com.test.web"})
- public class AppConfig extends WebMvcConfigurerAdapter {
- @Bean
- public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
- return new DeviceResolverHandlerInterceptor();
- }
- @Bean
- public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {
- return new DeviceHandlerMethodArgumentResolver();
- }
- @Bean
- public InternalResourceViewResolver getInternalResourceViewResolver() {
- InternalResourceViewResolver resolver = new InternalResourceViewResolver();
- resolver.setPrefix("/WEB-INF/");
- resolver.setSuffix(".jsp");
- return resolver;
- }
- @Override
- public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
- configurer.enable();
- }
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(deviceResolverHandlerInterceptor());
- }
- @Override
- public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
- argumentResolvers.add(deviceHandlerMethodArgumentResolver());
- }
- }
- /**
- * User: xiejx618
- * Date: 13-11-25
- * Time: 下午1:45
- */
- @Controller
- @RequestMapping("/sample")
- public class SampleController {
- @RequestMapping(value = "/test")
- public String test(Device device) {
- if (device.isMobile()) {
- System.out.println("Hello mobile user!");
- } else if (device.isTablet()) {
- System.out.println("Hello tablet user!");
- } else {
- System.out.println("Hello desktop user!");
- }
- return "test";
- }
- }
3.测试.用电脑,手机访问,看结果吧
源码:下载
http://download.csdn.net/detail/xiejx618/6615107
20131127小小改动
1.WebMvcConfigurerAdapter
- /**
- * User: xiejx618
- * Date: 13-11-25
- * Time: 下午1:53
- */
- @EnableWebMvc
- @Configuration
- @ComponentScan(basePackages={"com.test.web"})
- public class AppConfig extends WebMvcConfigurerAdapter {
- @Bean
- public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
- return new DeviceResolverHandlerInterceptor();
- }
- @Bean
- public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {
- return new DeviceHandlerMethodArgumentResolver();
- }
- @Bean
- public LiteDeviceDelegatingViewResolver getInternalResourceViewResolver() {
- InternalResourceViewResolver delegate = new InternalResourceViewResolver();
- delegate.setPrefix("/WEB-INF/");
- delegate.setSuffix(".jsp");
- LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(delegate);
- resolver.setMobilePrefix("mobile/");
- resolver.setTabletPrefix("tablet/");
- return resolver;
- }
- @Override
- public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
- configurer.enable();
- }
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(deviceResolverHandlerInterceptor());
- }
- @Override
- public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
- argumentResolvers.add(deviceHandlerMethodArgumentResolver());
- }
- }
2.controller
- /**
- * User: xiejx618
- * Date: 13-11-25
- * Time: 下午1:45
- */
- @Controller
- @RequestMapping("/sample")
- public class SampleController {
- @RequestMapping(value = "/test")
- public String test(Device device,HttpServletRequest request) {
- //可以在这做判断设备类型
- if (device.isMobile()) {
- request.setAttribute("device","Hello mobile user!");
- } else if (device.isTablet()) {
- request.setAttribute("device","Hello tablet user!");
- } else {
- request.setAttribute("device","Hello desktop user!");
- }
- //也可以根据不同的设备类型显示不同的view,详细见AppConfig的ViewResolver配置
- return "test";
- }
- }
3.测试.用电脑,手机访问,看结果吧
新源码:下载
http://download.csdn.net/detail/xiejx618/6618715
其它用法参考官网吧..