spring---mobile模块

来源:互联网 发布:java线程的同步问题 编辑:程序博客网 时间:2024/05/16 03:53

  在现如今的应用中,我们时常会区分访问应用的主体,将不同的访问主体进行分类统计,或根据访问主体返回不同页面。spring有一个mobile的模块可以区分访问主体是手机、平板、还是PC,接下来我们通过代码来看一下如何使用spring的mobile模块。

编码

添加依赖jar包:

<dependency>    <groupId>org.springframework.mobile</groupId>    <artifactId>spring-mobile-device</artifactId>    <version>1.1.3.RELEASE</version></dependency>

访问控制接口实现:

package deviceDetect;import org.springframework.mobile.device.Device;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class DeviceDetectionController {    @RequestMapping("detect-device")    public @ResponseBody String detectDevice(Device device){        String deviceType="unknown";        if(device.isNormal())            deviceType = "normal";//Pc端        else if (device.isMobile())            deviceType = "mobile";//手机端        else if (device.isTablet())            deviceType = "tablet";//平板        return "Hello " + deviceType + " browser!";    }}

可以看到,访问方法中有一个Device的接口类,这个类就是spring帮我们封装的访问主体信息,来一起让我们把程序跑起来,看一下运行结果:

package deviceDetect;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class,args);    }}

现在我们通过不同的设备访问,看一下运行结果吧!

参考:https://spring.io/guides

0 0