Java移动框架篇--Spring mobile简单试用

来源:互联网 发布:matlab两个数组相加 编辑:程序博客网 时间:2024/05/29 17:51

spring mobile 是spring新推出的一个用于支持移动浏览的小框架,用起来很简单,和spring mvc结合也很方便。


首先建立一个spring mvc的工程


然后,在pom.xml中添加spring mobile的支持

[html] view plain copy
  1. <dependency>  
  2.     <groupId>org.springframework.mobile</groupId>  
  3.     <artifactId>spring-mobile-device</artifactId>  
  4.     <version>1.1.0.RELEASE</version>  
  5. </dependency>  

修改servlet-content.xml

[html] view plain copy
  1. <!-- Enables the Spring MVC @Controller programming model -->  
  2. <annotation-driven>  
  3.     <argument-resolvers>  
  4.         <beans:bean  
  5.             class="org.springframework.mobile.device.DeviceWebArgumentResolver" />  
  6.         <beans:bean  
  7.             class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />  
  8.     </argument-resolvers>  
  9. </annotation-driven>  
  10.   
  11. <interceptors>  
  12.     <!-- On pre-handle, resolve the device that originated the web request -->  
  13.     <beans:bean  
  14.         class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />  
  15.     <!-- On pre-handle, manage the user's site preference (declare after DeviceResolverHandlerInterceptor) -->  
  16.     <beans:bean  
  17.         class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />  
  18. </interceptors>  
  19.   
  20. <!-- Handles HTTP GET requests for /resources/** by efficiently serving   
  21.     up static resources in the ${webappRoot}/resources directory -->  
  22. <resources mapping="/resources/**" location="/resources/" />  
  23.   
  24. <!-- Resolves views selected for rendering by @Controllers to .jsp resources   
  25.     in the /WEB-INF/views directory -->  
  26.     <beans:bean class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">  
  27.     <beans:constructor-arg>  
  28.         <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  29.             <beans:property name="prefix" value="/WEB-INF/views/" />  
  30.             <beans:property name="suffix" value=".jsp" />  
  31.         </beans:bean>  
  32.     </beans:constructor-arg>  
  33.     <beans:property name="enableFallback" value="true" />  
  34.     <beans:property name="mobilePrefix" value="mobile/" />  
  35.     <beans:property name="tabletPrefix" value="tablet/" />  
  36. </beans:bean>  

然后在views目录下,新增两个目录:mobile和tablet。拷贝home.jsp页面进去。


为了区分,分别修改3个home.jsp,如下

home.jsp

[html] view plain copy
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ page session="false" %>  
  3. <html>  
  4. <head>  
  5.     <title>Home</title>  
  6. </head>  
  7. <body>  
  8. <h1>  
  9.     Hello world!    
  10. </h1>  
  11. <P>  This is normal. </P>  
  12. <P>  The time on the server is ${serverTime}. </P>  
  13. </body>  
  14. </html>  

moblie/home.jsp

[html] view plain copy
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ page session="false" %>  
  3. <html>  
  4. <head>  
  5.     <title>Home</title>  
  6. </head>  
  7. <body>  
  8. <h1>  
  9.     Hello world!    
  10. </h1>  
  11. <P>  This is mobile. </P>  
  12. <P>  The time on the server is ${serverTime}. </P>  
  13. </body>  
  14. </html>  

tablet/home.jsp

[html] view plain copy
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ page session="false" %>  
  3. <html>  
  4. <head>  
  5.     <title>Home</title>  
  6. </head>  
  7. <body>  
  8. <h1>  
  9.     Hello world!    
  10. </h1>  
  11. <P>  This is tablet. </P>  
  12. <P>  The time on the server is ${serverTime}. </P>  
  13. </body>  
  14. </html>  

这样就完成了,容易吧。

当检测到时手机访问的时候,会自动将view映射到moblie目录下,当检测到平板访问的时候会将view映射到tablet目录下,其他时候,映射到根目录下。

这是电脑访问的样子


这是手机访问的样子

很方便呢。spring mobile还提供了其他的几种方式,在官网有个例子的链接,很直观。https://github.com/spring-projects/spring-mobile-samples

阅读全文
0 0
原创粉丝点击