spring mobile 是spring新推出的一个用于支持移动浏览的小框架,用起来很简单,和spring mvc结合也很方便。
首先建立一个spring mvc的工程
然后,在pom.xml中添加spring mobile的支持
- <dependency>
- <groupId>org.springframework.mobile</groupId>
- <artifactId>spring-mobile-device</artifactId>
- <version>1.1.0.RELEASE</version>
- </dependency>
修改servlet-content.xml
- <!-- Enables the Spring MVC @Controller programming model -->
- <annotation-driven>
- <argument-resolvers>
- <beans:bean
- class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
- <beans:bean
- class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
- </argument-resolvers>
- </annotation-driven>
- <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" />
- </interceptors>
- <!-- Handles HTTP GET requests for /resources/** by efficiently serving
- up static resources in the ${webappRoot}/resources directory -->
- <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/views/" />
- <beans:property name="suffix" value=".jsp" />
- </beans:bean>
- </beans:constructor-arg>
- <beans:property name="enableFallback" value="true" />
- <beans:property name="mobilePrefix" value="mobile/" />
- <beans:property name="tabletPrefix" value="tablet/" />
- </beans:bean>
然后在views目录下,新增两个目录:mobile和tablet。拷贝home.jsp页面进去。
为了区分,分别修改3个home.jsp,如下
home.jsp
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ page session="false" %>
- <html>
- <head>
- <title>Home</title>
- </head>
- <body>
- <h1>
- Hello world!
- </h1>
- <P> This is normal. </P>
- <P> The time on the server is ${serverTime}. </P>
- </body>
- </html>
moblie/home.jsp
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ page session="false" %>
- <html>
- <head>
- <title>Home</title>
- </head>
- <body>
- <h1>
- Hello world!
- </h1>
- <P> This is mobile. </P>
- <P> The time on the server is ${serverTime}. </P>
- </body>
- </html>
tablet/home.jsp
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ page session="false" %>
- <html>
- <head>
- <title>Home</title>
- </head>
- <body>
- <h1>
- Hello world!
- </h1>
- <P> This is tablet. </P>
- <P> The time on the server is ${serverTime}. </P>
- </body>
- </html>
这样就完成了,容易吧。
当检测到时手机访问的时候,会自动将view映射到moblie目录下,当检测到平板访问的时候会将view映射到tablet目录下,其他时候,映射到根目录下。
这是电脑访问的样子
这是手机访问的样子
很方便呢。spring mobile还提供了其他的几种方式,在官网有个例子的链接,很直观。https://github.com/spring-projects/spring-mobile-samples