Loading...
Spring MVC项目添加Spring mobile支持

Spring专题 2015/12/18 Spring Framework , Spring MVC



1、添加jar包支持

//Spring mobile

compile 'org.springframework.mobile:spring-mobile-device:1.1.3.RELEASE'


2、Spring MVC配置文件添加如下内容 
<mvc:interceptors>
 <!-- On pre-handle, resolve the device that originated the web request -->
 <mvc:interceptor>
  <mvc:mapping path="/mobile/**"/>
    <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
   </mvc:interceptor> 
</mvc:interceptors> 

<mvc:annotation-driven>
 <mvc:argument-resolvers>
     <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
 </mvc:argument-resolvers>
</mvc:annotation-driven>


3、控制器代码

@Controller

@RequestMapping("/mobile")

public class MobileController {

 

    @RequestMapping("")

    public String mobile(Device device,Model model) {

        String message = "";

        if (device.isMobile()) {

        System.out.println(message = "Hello mobile user!");

        } else if (device.isTablet()) {

        System.out.println(message = "Hello tablet user!");

        } else {

        System.out.println(message = "Hello desktop user!");

        }

        model.addAttribute("message", message);

        return "mobile/mobile";

    }

}

 


更多详细配置请看:http://projects.spring.io/spring-mobile/


Comments