`
zhoujiangzi
  • 浏览: 92717 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring 判断请求终端类型

 
阅读更多

今天在看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请求参数与响应结果全局加密和解密详解

    Spring MVC请求参数与响应结果全局加密和解密详解 在本文中,我们将详细介绍Spring MVC请求参数与响应结果全局加密和解密的相关知识点,包括请求参数的加密和解密、响应结果的加密和解密、ContentType的处理等。 ...

    4中url请求方式,包括http和spring自带请求

    本文将深入探讨四种主要的URL请求方式,包括HTTP基础请求方法以及Spring框架中的请求工具类。我们将讨论它们的工作原理、优缺点以及如何在实际开发中灵活运用。 1. HTTP基础请求方法: - GET:最常用的方法,用于...

    SpringBoot+SpringSecurity处理Ajax登录请求问题(推荐)

    SpringBoot+SpringSecurity处理Ajax登录请求问题 SpringBoot+SpringSecurity处理Ajax登录请求问题是SpringBoot开发中的一個常见问题,本文将详细介绍如何使用SpringBoot+SpringSecurity处理Ajax登录请求问题。 ...

    Spring MVC请求映射常见的三种方式

    本文将详细探讨Spring MVC中常见的三种请求映射方式:基于注解的映射、基于XML配置的映射以及基于Java配置的映射。我们将深入理解每种方式的工作原理,并通过实例来展示其用法。 ### 1. 基于注解的映射 **注解驱动...

    springmvc+spring线程池处理http并发请求数据同步控制问题

    同时,Spring框架提供的线程池功能则可以帮助我们优化多线程环境下的性能,特别是处理并发请求时。在这个主题中,我们将深入探讨如何利用Spring MVC与Spring线程池来有效地管理并发请求,并解决数据同步控制问题。 ...

    SpringCloud Finchley Gateway 缓存请求Body和Form表单的实现

    总之,Spring Cloud Gateway在处理请求时提供了高度的灵活性,但同时也要求开发者对响应式编程模型有深刻的理解。通过自定义`GlobalFilter`,我们可以实现对特定类型请求数据的缓存,这在某些业务场景下是十分有用的...

    Spring web请求异常拦截统一返回

    Spring web请求异常拦截统一返回

    SPRING MVC 的请求参数获取的几种方法

    SPRING MVC 请求参数获取的几种方法 SPRING MVC 框架中,获取请求参数是非常重要的一步,下面将介绍 SPRING MVC 中获取请求参数的几种方法。 1. 使用 @PathVariable 注解获取路径中传递参数 在 SPRING MVC 中,...

    spring security ajax请求与html共存

    当Ajax请求被拒绝时,Spring Security默认会重定向到一个错误页面,但这对Ajax请求并不适用。因此,我们需要提供一个错误处理器,将错误信息作为JSON或其他适合Ajax响应的格式返回。 5. **HTML与Ajax共存** 在...

    Spring MVC 配置请求的默认处理器.rar

    在Spring MVC框架中,配置请求的默认处理器是构建Web应用程序的关键步骤。这个过程涉及到设置DispatcherServlet,它作为Spring MVC的核心组件,负责调度HTTP请求到相应的控制器。以下将详细阐述Spring MVC配置请求的...

    Spring拦截器,高级参数绑定

    高级参数绑定是指在Controller方法中,Spring MVC能够自动将请求参数绑定到方法参数上,包括基本类型、复杂对象甚至自定义类型。例如,可以使用`@RequestParam`、`@PathVariable`、`@RequestHeader`、`@RequestBody`...

    Spring Security 源码

    为了防止跨站请求伪造(CSRF)攻击,Spring Security提供了内置的CSRF保护,通过生成和验证CSRF令牌来确保只有合法的请求被处理。 8. **OAuth2集成**: Spring Security支持OAuth2协议,允许第三方应用通过授权...

    spring-boot 自定义xml配置web请求拦截器

    标题中的"spring-boot 自定义xml配置web请求拦截器"指的是在Spring Boot项目中,通过XML配置方式实现对Web请求的拦截处理。这涉及到Spring Boot的Web层架构、AOP(面向切面编程)以及自定义拦截器的概念。Spring ...

    spring boot整合shiro实现url请求过滤

    本demo为Spring boot整合shiro,以mybatis plus做dao层交互数据,实现了读取数据库用户数据实现用户登录,权限认证,读取数据库中用户对应的url请求,实现请求的过滤。自定义了relam和过滤器来实现这些功能

    最全的Spring MVC注解例子,异步请求,错误处理

    在这个“最全的Spring MVC注解例子”中,我们将深入探讨Spring MVC的核心注解,以及如何实现异步请求处理和错误管理。 1. **Spring MVC核心注解** - `@Controller`:标记一个类为处理HTTP请求的控制器。这是Spring...

    基于Spring MVC接收并处理HTTP请求

    Spring MVC提供了一种优雅的方式来处理应用中的异常,通过@ControllerAdvice和@ExceptionHandler注解,可以全局处理特定类型的异常。 7. **源码分析** 深入源码层面,我们可以看到DispatcherServlet是如何通过...

    spring-mock.jar

    Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi....

    spring mvc 拦截器获取请求数据信息.rar

    1. `preHandle`: 这个方法在请求处理之前被调用,返回值为布尔类型,如果返回`true`,则表示允许继续处理请求;如果返回`false`,请求将会被中断。 2. `postHandle`: 这个方法在控制器处理完请求后,但在视图渲染...

    spring2.0升级到spring3.0.5的开发包

    4. **Spring MVC增强**:Spring Web MVC在3.0版本中得到了显著增强,包括支持RESTful风格的URL映射、支持异步请求处理、模板引擎集成(如FreeMarker、Thymeleaf)以及改进的视图解析。 5. **数据访问增强**:Spring...

    springcloud下通过gateway转发websocket

    在IT行业中,Spring Cloud Gateway作为Spring Cloud生态...结合Spring Cloud Gateway的路由和过滤器功能,不仅可以方便地转发WebSocket请求,还可以在请求处理过程中实现各种高级功能,提高系统的灵活性和可扩展性。

Global site tag (gtag.js) - Google Analytics