`
一缕疯狂0
  • 浏览: 5199 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

spring mobile -- hello world

 
阅读更多
  • 搭建一个web项目
  • 添加maven管理
  • maven引入相关包
  • 配置web.xml
  • 配置xxx-servlet.xml
  • SpringMobileHelloController.java
  • 添加mobile、tablet 相关页面
  • 测试访问

1,2 这里就不介绍了,从3开始

 

  • maven导入相关包
<dependencies>
		<!-- spring lib -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<!-- spring mobile -->
		<dependency>
			<groupId>org.springframework.mobile</groupId>
			<artifactId>spring-mobile-device</artifactId>
			<version>1.1.3.RELEASE</version>
		</dependency>
		<!-- logger lib -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.2</version>
		</dependency>
	</dependencies>

 

  • 配置web.xml
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <session-config>   
		<session-timeout>600</session-timeout>   
	</session-config>
    <context-param>
	   <param-name>contextConfigLocation</param-name>
	   <param-value>classpath:/*applicationContext.xml</param-value>
	</context-param>
	
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>   
	
	<servlet>
        <servlet-name>xxx</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	
	<servlet-mapping>
        <servlet-name>xxx</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>

 

 

  • 配置xxx-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
			http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

	<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
		<property name="interceptors"> <list> <bean class="com.zadobo.base.system.web.AuthorizeInterceptor"/> 
		</list> </property> </bean> -->
	<mvc:annotation-driven>
		<mvc:argument-resolvers>
			<bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
			<bean
				class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
		</mvc:argument-resolvers>
	</mvc:annotation-driven>

	<context:annotation-config />

	<mvc:interceptors>
		<!-- On pre-handle, resolve the device that originated the web request -->
		<bean
			class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
		<!-- On pre-handle, manage the user's site preference (declare after DeviceResolverHandlerInterceptor) -->
		<bean
			class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />
	</mvc:interceptors>
	<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
		up static resources in the ${webappRoot}/resources directory -->
	<mvc:resources mapping="/resources/**" location="/resources/" />

	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	<bean
		class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
		<constructor-arg>
			<bean id="viewResolver"
				class="org.springframework.web.servlet.view.InternalResourceViewResolver"
				p:prefix="/WEB-INF/" p:suffix=".jsp">
			</bean>
		</constructor-arg>
		<property name="enableFallback" value="true" />
		<property name="mobilePrefix" value="mobile/" />
		<property name="tabletPrefix" value="tablet/" />
	</bean>

	<bean id="exceptionResolver"
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.Exception">/util/errorPage</prop>
				<prop key="java.lang.SecurityException">/util/exception/securityException</prop>
			</props>
		</property>
	</bean>
	<context:component-scan base-package="com.test.web" />
</beans>

 

 

  • SampleController.java
package com.test.web;

import org.springframework.mobile.device.Device;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/sample")
public class SampleController {
	@RequestMapping(value = "/test.htm")
	public String test(Device device) {
		if (device.isMobile()) {
			System.out.println("Hello mobile user!");
		} else if (device.isTablet()) {
			System.out.println("Hello tablet user!");
		} else {
			System.out.println("Hello desktop user!");
		}
		return "index";
	}
}

 

  • 页面结构



 

  • 大小: 4.8 KB
分享到:
评论

相关推荐

    SpringMVC ---- HelloWorld ---- 代码

    SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- HelloWorld ---- 代码 SpringMVC ---- Hello...

    spring-boot-01-helloworld.zip

    Spring Boot学习笔记-------(二)spring boot入门,配套例子代码,博客地址:https://blog.csdn.net/huaya1127/article/details/104130300

    spring-rabbitmq-helloworld

    spring和rabbitmq整合的helloworld

    spring4-mvc-gradle-xml-hello-world, Gradle + spring 4 MVC Hello World 示例( XML ).zip

    spring4-mvc-gradle-xml-hello-world, Gradle + spring 4 MVC Hello World 示例( XML ) Gradle --spring-4 MVC Hello Worldspring 4 MVC + JSP视图+ XML配置模板,使用Gradle构建工具。使用的###1. 技术Gradle 2.0...

    spring-security-helloworld-annotation

    本文将深入探讨一个名为"spring-security-helloworld-annotation"的示例,该示例展示了如何使用注解来配置Spring Security。我们将从基本概念、核心组件到实际应用,全面解析Spring Security的注解使用。 1. **注解...

    spring-mvc-helloworld

    这个项目 "spring-mvc-helloworld" 是一个基础的 Spring MVC 示例,用于帮助初学者理解其工作原理。 1. **MVC 架构模式**: MVC 是一种设计模式,用于分离应用程序的数据、业务逻辑和用户界面。Model 负责处理数据...

    Spring MVC--1.helloworld

    Spring MVC--1.helloworld

    spring-mobile-device.jar

    用于java后台识别手机或电脑访问,jar包的简单使用可以访问博客https://blog.csdn.net/weixin_42286461/article/details/88547200

    springcloud-nacos-helloworld-master.zip

    1. 先配置 nacos server 2. 启动 ProviderApplication.java类 3. 启动 ConsumerApplication.java类 4. 浏览器中输入 http://localhost:8082/say/hello

    spring-boot-helloworld

    在“spring-boot-helloworld”项目中,我们看到这是一个入门级的Spring Boot应用,虽然规模不大,但包含了Spring Boot的核心特性,为初学者提供了很好的学习模板。下面我们将深入探讨其中的关键知识点。 1. **...

    spring-security-helloworld

    【标题】"spring-security-helloworld" 是一个基于Spring Security框架的简单示例项目,它用于初学者理解并实践Spring Security的基础用法。Spring Security是一个强大的安全框架,为Java应用程序提供了全面的安全...

    spring-boot-helloworld.zip

    在 "spring-boot-helloworld.zip" 这个压缩包中,我们很可能是找到了一篇关于 Spring Boot 入门的博客文章示例代码,用于展示如何构建一个简单的 "Hello World" 应用。 在 Spring Boot 中创建一个 "Hello World" ...

    spring-core-x.x.x.jar 包下载

    https://blog.csdn.net/Hello_World_QWP/article/details/80652618 jar包中的版本具体如下: spring-core-4.1.6.RELEASE.jar spring-core-4.3.10.RELEASE.jar spring-core-4.3.12.RELEASE.jar spring-core-4.3.13....

    Spring-Security-3-HelloWorld 实例简单代码

    在这个"Spring-Security-3-HelloWorld"实例中,我们将探讨如何在Spring MVC应用中集成Spring Security的基本配置和用法。 首先,Spring Security的核心功能包括用户认证、权限授权、CSRF保护、会话管理等。在Spring...

    Spring3MVC-REST-HelloWorld 实例简单代码

    Spring3MVC-REST-HelloWorld 是一个基础的示例,用于展示如何在Spring框架的MVC模块中实现RESTful Web服务。这个实例是初学者理解Spring MVC与REST结合使用的理想起点。REST(Representational State Transfer)是一...

    spring boot - hello world

    例如,要创建一个简单的web应用,我们可以添加`spring-boot-starter-web`依赖。这将包含Spring MVC和Tomcat,使得我们可以立刻开始编写web应用。 2. **主应用类(Main Application Class)** 在"Hello World"示例...

    spring-boot的helloWorld

    这个简单的"spring-boot-01-helloworld"程序,是学习Spring Boot的起点,它展示了如何快速构建一个可以运行的web应用。随着对Spring Boot的理解加深,你可以逐渐添加更多功能,如数据库操作、安全控制、定时任务等,...

    SpringData-1 概述与HelloWorld

    - 使用`mvn spring-boot:run`启动应用,通过`curl`或Postman测试API接口,观察返回结果。 通过以上步骤,你将能够理解SpringData JPA的基本用法,并能快速地进行数据库操作。随着对SpringData JPA的深入学习,你...

    spring-boot-hello-world:Spring Boot 应用程序示例

    "spring-boot-hello-world"项目就是一个很好的例子,展示了Spring Boot的基础用法。 1. **项目结构解析** - "spring-boot-hello-world-master"目录下的项目通常包含以下关键组件: - `pom.xml`:这是Maven的项目...

    spring-boot-starter-hello.zip

    spring-boot-starter-hello是自己新建的自定义的spring-boot-starter 具体使用参考博文https://blog.csdn.net/fighterandknight/article/details/90731821

Global site tag (gtag.js) - Google Analytics