• spring mvc+spring mobile简单的用法


    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

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. /** 
    2.  * User: xiejx618 
    3.  * Date: 13-11-25 
    4.  * Time: 下午1:53 
    5.  */  
    6. @EnableWebMvc  
    7. @Configuration  
    8. @ComponentScan(basePackages={"com.test.web"})  
    9. public class AppConfig extends WebMvcConfigurerAdapter {  
    10.   
    11.     @Bean  
    12.     public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {  
    13.         return new DeviceResolverHandlerInterceptor();  
    14.     }  
    15.   
    16.     @Bean  
    17.     public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {  
    18.         return new DeviceHandlerMethodArgumentResolver();  
    19.     }  
    20.   
    21.     @Bean  
    22.     public InternalResourceViewResolver getInternalResourceViewResolver() {  
    23.         InternalResourceViewResolver resolver = new InternalResourceViewResolver();  
    24.         resolver.setPrefix("/WEB-INF/");  
    25.         resolver.setSuffix(".jsp");  
    26.         return resolver;  
    27.     }  
    28.   
    29.     @Override  
    30.     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {  
    31.         configurer.enable();  
    32.     }  
    33.   
    34.     @Override  
    35.     public void addInterceptors(InterceptorRegistry registry) {  
    36.         registry.addInterceptor(deviceResolverHandlerInterceptor());  
    37.     }  
    38.   
    39.     @Override  
    40.     public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {  
    41.         argumentResolvers.add(deviceHandlerMethodArgumentResolver());  
    42.     }  
    43. }  
    2.controller

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. /** 
    2.  * User: xiejx618 
    3.  * Date: 13-11-25 
    4.  * Time: 下午1:45 
    5.  */  
    6. @Controller  
    7. @RequestMapping("/sample")  
    8. public class SampleController {  
    9.     @RequestMapping(value = "/test")  
    10.     public String test(Device device) {  
    11.         if (device.isMobile()) {  
    12.             System.out.println("Hello mobile user!");  
    13.         } else if (device.isTablet()) {  
    14.             System.out.println("Hello tablet user!");  
    15.         } else {  
    16.             System.out.println("Hello desktop user!");  
    17.         }  
    18.         return "test";  
    19.     }  
    20. }  

    3.测试.用电脑,手机访问,看结果吧

    源码:下载

    http://download.csdn.net/detail/xiejx618/6615107


    20131127小小改动

    1.WebMvcConfigurerAdapter

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. /** 
    2.  * User: xiejx618 
    3.  * Date: 13-11-25 
    4.  * Time: 下午1:53 
    5.  */  
    6. @EnableWebMvc  
    7. @Configuration  
    8. @ComponentScan(basePackages={"com.test.web"})  
    9. public class AppConfig extends WebMvcConfigurerAdapter {  
    10.   
    11.     @Bean  
    12.     public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {  
    13.         return new DeviceResolverHandlerInterceptor();  
    14.     }  
    15.   
    16.     @Bean  
    17.     public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {  
    18.         return new DeviceHandlerMethodArgumentResolver();  
    19.     }  
    20.   
    21.     @Bean  
    22.     public LiteDeviceDelegatingViewResolver getInternalResourceViewResolver() {  
    23.         InternalResourceViewResolver delegate = new InternalResourceViewResolver();  
    24.         delegate.setPrefix("/WEB-INF/");  
    25.         delegate.setSuffix(".jsp");  
    26.         LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(delegate);  
    27.         resolver.setMobilePrefix("mobile/");  
    28.         resolver.setTabletPrefix("tablet/");  
    29.         return resolver;  
    30.     }  
    31.   
    32.     @Override  
    33.     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {  
    34.         configurer.enable();  
    35.     }  
    36.   
    37.     @Override  
    38.     public void addInterceptors(InterceptorRegistry registry) {  
    39.         registry.addInterceptor(deviceResolverHandlerInterceptor());  
    40.     }  
    41.   
    42.     @Override  
    43.     public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {  
    44.         argumentResolvers.add(deviceHandlerMethodArgumentResolver());  
    45.     }  
    46. }  

    2.controller

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. /** 
    2.  * User: xiejx618 
    3.  * Date: 13-11-25 
    4.  * Time: 下午1:45 
    5.  */  
    6. @Controller  
    7. @RequestMapping("/sample")  
    8. public class SampleController {  
    9.     @RequestMapping(value = "/test")  
    10.     public String test(Device device,HttpServletRequest request) {  
    11.         //可以在这做判断设备类型  
    12.         if (device.isMobile()) {  
    13.             request.setAttribute("device","Hello mobile user!");  
    14.         } else if (device.isTablet()) {  
    15.             request.setAttribute("device","Hello tablet user!");  
    16.         } else {  
    17.             request.setAttribute("device","Hello desktop user!");  
    18.         }  
    19.         //也可以根据不同的设备类型显示不同的view,详细见AppConfig的ViewResolver配置  
    20.         return "test";  
    21.     }  
    22. }  

    3.测试.用电脑,手机访问,看结果吧

    新源码:下载

    http://download.csdn.net/detail/xiejx618/6618715

    我觉得还是不要配LiteDeviceDelegatingViewResolver这个,配了它,如果没有被@ResponseBody打断就转到viewresovler,要考虑每个requestmappingl是否都需要三个view.如果只有一两个页面根据不同设备访问不同页面的,在上面判断设备后可以用return "redirect:xxview",就简单多了

    其它用法参考官网吧..

  • 相关阅读:
    python正则表达式(+ {})(二)
    14丨 HTTP有哪些优点?又有哪些缺点?
    python正则表达式(. *)(一)
    12丨响应状态码该怎么用?
    Fiddler—Fiddler+willow插件应用(十四)
    11丨你能写出正确的网址吗?
    【洛谷P1858】多人背包
    【洛谷P3387】(模板)缩点
    【洛谷P2184】贪婪大陆
    Leetcode: 39. Combination Sum
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318043.html
Copyright © 2020-2023  润新知