今天在看spring mobile时,想通过spring为我们提供的Device接口来判断请求的终端类型
这个首先需要加入spring-mobile的jar,对应maven:
<dependency> <groupId>org.springframework.mobile</groupId> <artifactId>spring-mobile-device</artifactId> <version>${spring.mobile.version}</version> </dependency>
当前的版本是:<spring.mobile.version>1.1.0.RELEASE</spring.mobile.version>
接着在spring-mvc.xml文档中添加mobile的配置,首先需要将xsd文件升级到3.2,加入视图处理,如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" default-lazy-init="true"> <mvc:annotation-driven> <mvc:argument-resolvers> <beans:bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" /> <beans:bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" /> </mvc:argument-resolvers> </mvc:annotation-driven> <mvc:interceptors> <!-- On pre-handle, resolve the device that originated the web request --> <beans:bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" /> <!-- On pre-handle, manage the user's site preference (declare after DeviceResolverHandlerInterceptor) --> <beans:bean class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" /> </mvc:interceptors> <!-- 自动注册bean,排除contrller注解 --> <context:component-scan base-package="com.jacksoft.ispring.mvc.controller" > </context:component-scan> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <mvc:resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver"> <beans:constructor-arg> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/jsp/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> </beans:constructor-arg> <beans:property name="mobilePrefix" value="mobile/" /> <beans:property name="tabletPrefix" value="tablet/" /> </beans:bean> </beans>
这样可以区分平板,电脑,手机设备。
实际上Spring是通过org.springframework.mobile.device.DeviceResolverHandlerInterceptor 这个拦截器来获取当前访问的User-Agent,通过这个来判断具体的访问设备,然后将这个设备存放到HttpServletRequest中,这样我们就可以通过这个来进行判断了。
/* * Copyright 2010-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.mobile.device; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; /** * A Spring MVC interceptor that resolves the Device that originated the web request <i>before</i> any request handler is invoked. * The resolved Device is exported as a request attribute under the well-known name of {@link DeviceUtils#CURRENT_DEVICE_ATTRIBUTE}. * Request handlers such as @Controllers and views may then access the currentDevice to vary their control and rendering logic, respectively. * @author Keith Donald */ public class DeviceResolverHandlerInterceptor extends HandlerInterceptorAdapter { private final DeviceResolver deviceResolver; /** * Create a device resolving {@link HandlerInterceptor} that defaults to a {@link LiteDeviceResolver} implementation. */ public DeviceResolverHandlerInterceptor() { this(new LiteDeviceResolver()); } /** * Create a device resolving {@link HandlerInterceptor}. * @param deviceResolver the device resolver to delegate to in {@link #preHandle(HttpServletRequest, HttpServletResponse, Object)}. */ public DeviceResolverHandlerInterceptor(DeviceResolver deviceResolver) { this.deviceResolver = deviceResolver; } public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Device device = deviceResolver.resolveDevice(request); request.setAttribute(DeviceUtils.CURRENT_DEVICE_ATTRIBUTE, device); return true; } }
这样我们就可以在WEB-INF/jsp/ 下面创建一个test.jsp和mobile/test.jsp页面了,这样通过电脑PC来访问的时候,会自动返回WEB-INF下面的test.jsp,而通过手机来访问的时候是返回mobile/test.jsp
这样就可以对不同的终端进行适配了。
相关推荐
主要给大家介绍了关于Spring MVC请求参数与响应结果全局加密和解密的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
该资源提供4中url请求方式,各有不同优缺点,可自行选择,参数可格式话,可拼接
主要介绍了SpringBoot+SpringSecurity处理Ajax登录请求问题,本文给大家介绍的非常不错,具有参考借鉴价值,需要的朋友可以参考下
Spring web请求异常拦截统一返回
springmvc+spring线程池处理http并发请求数据同步控制问题
慨括了spring mvc请求参数常见的几种获取方法
在接入Spring-Cloud-Gateway时,可能有需求进行缓存Json-Body数据或者Form-Urlencoded数据的情况。这篇文章主要介绍了SpringCloud Finchley Gateway 缓存请求Body和Form表单的实现,感兴趣的小伙伴们可以参考一下
Spring事务类型祥解讲解各种设置
spring mvc 拦截器获取请求数据信息 解压之后放到项目中 直接运行就可以了 (将流多次运用)
NULL 博文链接:https://xieke90.iteye.com/blog/2235706
本demo为Spring boot整合shiro,以mybatis plus做dao层交互数据,实现了读取数据库用户数据实现用户登录,权限认证,读取数据库中用户对应的url请求,实现请求的过滤。自定义了relam和过滤器来实现这些功能
Spring MVC 配置请求的默认处理器.rar
Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi....
Spring如何装配各种集合类型的属性
spring security 安全认证与资源管理,,安全性特别高,,可以配置更多的安全设置。在许多企业应用中,都选用了这项技术。该代码主要将ajax请求与from请求区别。供大家参考!
spring boot 支持跨域 前台不需要jsonp 请求 正常js即可 spring boot 支持跨域 前台不需要jsonp 请求 正常js即可
cxf 请求webService 方式 xml
6.Spring RestTemplate中几种常见的请求方式 7.RestTemplate的逆袭之路,从发送请求到负载均衡 8.Spring Cloud中负载均衡器概览 9.Spring Cloud中的负载均衡策略 10.Spring Cloud中的断路器Hystrix 11.Spring ...
spring boot+spring mvc+spring代理请求**音乐接口获取数据,然后提供给**小程序做显示 运行环境 jdk8+IntelliJ IDEA+maven 项目技术(必填) spring boot+spring mvc+spring 数据库文件 无,通过**音乐接口获取...
主要介绍了Java Spring Controller 获取请求参数的几种方法详解的相关资料,这里提供了6种方法,需要的朋友可以参考下