今天在看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
这样就可以对不同的终端进行适配了。