• spring mobile简单试用


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


    首先建立一个spring mvc的工程


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

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    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 plaincopy在CODE上查看代码片派生到我的代码片
    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 plaincopy在CODE上查看代码片派生到我的代码片
    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 plaincopy在CODE上查看代码片派生到我的代码片
    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 plaincopy在CODE上查看代码片派生到我的代码片
    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

  • 相关阅读:
    初探深度学习
    第二次作业:卷积神经网络 part 1
    随笔小记
    Typora实用小工具(AHK)
    第一次作业:深度学习基础
    Nginx HTTPS 部署实战
    win10系统中如何解决cmd中的路径和现在电脑的用户名不一致
    Centos7.5.1804永久生效修改主机名
    centos6.9NAT网络模式
    linux 系统中用root切换到普通用户时显示的异常如bash4.1$
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318042.html
Copyright © 2020-2023  润新知