首页 > 其他分享 >spring mobile简单试用

spring mobile简单试用

时间:2023-01-04 13:33:34浏览次数:37  
标签:mobile spring springframework jsp 试用 org home


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


首先建立一个spring mvc的工程

spring mobile简单试用_java


然后,在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页面进去。

spring mobile简单试用_mobile_02


为了区分,分别修改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简单试用_mobile_03


这是手机访问的样子

spring mobile简单试用_spring_04

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




标签:mobile,spring,springframework,jsp,试用,org,home
From: https://blog.51cto.com/u_15929643/5988326

相关文章

  • Spring Cloud GateWay基于nacos如何去做灰度发布
    本文基于ReactiveLoadBalancerClientFilter使用RoundRobinLoadBalancer灰度发布灰度发布,又称为金丝雀发布,是一种新旧版本平滑发布的方式。在上面可以对同一个API进行两个版......
  • spring boot改2.5.9,Junit4升级成Junit5
    spring-boot从2.2.10.RELEASE改到2.5.9时,发现mvntest,单元测试没有跑,百度一圈,发现  springboot2.2之前使用的是Junit4  springboot2.2之后使用的是Junit5......
  • springboot连接sqlserver报错
    报错1:TheserverselectedprotocolversionTLS10isnotaccepted  解决:找到Java的java.security文件,并删除“TLSv1,TLSv1.1,3DES_EDE_CBC”,这三个参数:  报......
  • 回归测试用例选择方法
    1、回归测试就是修改完bug后对程序的新一轮测试。根据微软的统计,按照他们的经验,一般开发人员解决3~4个bug会衍生出一个新的bug,这就是必须作回归测试的原因。2......
  • spring-boot:run 中文乱码
    <plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId>......
  • Spring Cloud Alibaba组件之Sentinel
    目录一引入Sentinel学习二Sentinel入门三搭建Sentinel Dashboard四Springboot项目接入Sentinel五接入限流埋点六限流配置七熔断降级八熔断降级SpringClo......
  • spring整合Mybatis | Postgresql为例
    1.创建配置文件jdbc.propertiesjdbc.url=jdbc:postgresql://localhost:5432/postgis_hy?useSSL=falsejdbc.username=postgresjdbc.password=arcgis2.相关依赖<de......
  • spring boot——spring boot的基本配置——spring boot整合mybatis——本地实例运行—
    pojo类:packageorg.example.entity;publicclassMyUser{privateintid;privateStringname;privateintage;publicintgetId(){......
  • Spring IOC官方文档学习笔记(六)之自定义bean的特性
    1.生命周期回调(1)如果我们想要介入bean的生命周期,可通过实现spring中的InitializingBean和DisposableBean接口来达到这一目的,spring会调用InitializingBean中的afterPro......
  • Spring依赖查找
    SpringFramework提供了以下几种依赖查找Bean的方式1.按名称进行查找按id和name查找都归属于此。具体的又会分为直接查找与间接查找的方式。1.1直接查找以xml为例,新......