Permalink
Browse files

MOBILE-59 Added sample demonstrating LiteDeviceDelegatingViewResolver

  • Loading branch information...
royclarkson committed Jan 14, 2013
1 parent 14615b6 commit 5fbaf55c73ab663b5361ae232f6f52851fe31711
Showing with 590 additions and 3 deletions.
  1. +27 −0 lite-device-delegating-view-resolver/README.md
  2. +16 −0 lite-device-delegating-view-resolver/pom.xml
  3. +41 −0 ...ce-delegating-view-resolver/src/main/java/org/springframework/showcases/lite/AboutController.java
  4. +41 −0 ...ice-delegating-view-resolver/src/main/java/org/springframework/showcases/lite/HomeController.java
  5. +29 −0 ...gating-view-resolver/src/main/java/org/springframework/showcases/lite/config/ComponentConfig.java
  6. +73 −0 ...e-delegating-view-resolver/src/main/java/org/springframework/showcases/lite/config/WebConfig.java
  7. +41 −0 lite-device-delegating-view-resolver/src/main/resources/log4j.xml
  8. +3 −0 lite-device-delegating-view-resolver/src/main/webapp/META-INF/MANIFEST.MF
  9. +23 −0 lite-device-delegating-view-resolver/src/main/webapp/WEB-INF/views/about.jsp
  10. +23 −0 lite-device-delegating-view-resolver/src/main/webapp/WEB-INF/views/home.jsp
  11. +24 −0 lite-device-delegating-view-resolver/src/main/webapp/WEB-INF/views/mobile/about.jsp
  12. +24 −0 lite-device-delegating-view-resolver/src/main/webapp/WEB-INF/views/mobile/home.jsp
  13. +24 −0 lite-device-delegating-view-resolver/src/main/webapp/WEB-INF/views/tablet/about.jsp
  14. +24 −0 lite-device-delegating-view-resolver/src/main/webapp/WEB-INF/views/tablet/home.jsp
  15. +35 −0 lite-device-delegating-view-resolver/src/main/webapp/WEB-INF/web.xml
  16. +48 −0 ...elegating-view-resolver/src/test/java/org/springframework/showcases/lite/AboutControllerTest.java
  17. +48 −0 ...delegating-view-resolver/src/test/java/org/springframework/showcases/lite/HomeControllerTest.java
  18. +41 −0 lite-device-delegating-view-resolver/src/test/resources/log4j.xml
  19. +5 −3 pom.xml
@@ -0,0 +1,27 @@
+# Spring Mobile Lite Device Delegating View Resolver
+
+## Overview
+
+This sample app demonstrates many of the capabilities of the Spring Mobile Device module, including:
+
+* "Lite" device resolution through the use of DeviceResolverHandlerInterceptor
+* Site preference management
+* View resolution with LiteDeviceDelegatingViewResolver
+
+## Build and Run the Sample Application
+
+1. Build the app:
+
+ $ mvn clean install
+
+2. Deploy the .war to a Servlet 2.5 or > ServletContainer. This can be done via Maven on the command-line by running:
+
+ $ mvn tomcat:run
+
+ _Note: Alternatively, you may import the project into your IDE and deploy to a Servlet 2.5 or > container such as Tomcat 6 or 7._
+
+3. Access the project at the following URL:
+
+ http://localhost:8080/lite-device-delegating-view-resolver
+
+ _Note: Accessing this URL from your browser versus an Android emulator or iOS simulator will demonstrate the functionality_
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.springframework.mobile</groupId>
+ <artifactId>spring-mobile-samples</artifactId>
+ <version>1.1.0-BUILD-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>lite-device-delegating-view-resolver</artifactId>
+ <packaging>war</packaging>
+ <name>lite-device-delegating-view-resolver</name>
+
+</project>
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2010-2013 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.showcases.lite;
+
+import org.springframework.mobile.device.site.SitePreference;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+/**
+ * Handles requests for the About page.
+ * @author Roy Clarkson
+ */
+@Controller
+public class AboutController {
+
+ /**
+ * Show the About page to the user.
+ * Declares a {@link SitePreference} parameter to show how you can resolve the user's site preference.
+ * This controller renders a different version of the about view if the user has a mobile site preference.
+ */
+ @RequestMapping("/about")
+ public String home(SitePreference sitePreference, Model model) {
+ return "about";
+ }
+
+}
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2010-2013 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.showcases.lite;
+
+import org.springframework.mobile.device.site.SitePreference;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+/**
+ * Handles requests for the Home page.
+ * @author Roy Clarkson
+ */
+@Controller
+public class HomeController {
+
+ /**
+ * Show the home page to the user.
+ * Declares a {@link SitePreference} parameter to show how you can resolve the user's site preference.
+ * This controller renders a different version of the home view if the user has a mobile site preference.
+ */
+ @RequestMapping("/")
+ public String home(SitePreference sitePreference, Model model) {
+ return "home";
+ }
+
+}
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2010-2013 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.showcases.lite.config;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author Roy Clarkson
+ */
+@Configuration
+@ComponentScan(basePackages = "org.springframework.showcases.lite")
+public class ComponentConfig {
+
+}
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2010-2013 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.showcases.lite.config;
+
+import java.util.List;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;
+import org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor;
+import org.springframework.mobile.device.site.SitePreferenceHandlerMethodArgumentResolver;
+import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
+import org.springframework.web.method.support.HandlerMethodArgumentResolver;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
+import org.springframework.web.servlet.view.InternalResourceViewResolver;
+
+/**
+ * @author Roy Clarkson
+ * @see WebMvcConfigurer
+ */
+@Configuration
+@EnableWebMvc
+public class WebConfig extends WebMvcConfigurerAdapter {
+
+ // implementing WebMvcConfigurer
+
+ public void addInterceptors(InterceptorRegistry registry) {
+ registry.addInterceptor(new DeviceResolverHandlerInterceptor());
+ registry.addInterceptor(new SitePreferenceHandlerInterceptor());
+ }
+
+ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
+// argumentResolvers.add(new DeviceHandlerMethodArgumentResolver());
+ argumentResolvers.add(new SitePreferenceHandlerMethodArgumentResolver());
+ }
+
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
+ registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
+ }
+
+ // additional webmvc-related beans
+
+ @Bean
+ public LiteDeviceDelegatingViewResolver liteDeviceAwareViewResolver() {
+ InternalResourceViewResolver delegate = new InternalResourceViewResolver();
+ delegate.setPrefix("/WEB-INF/views/");
+ delegate.setSuffix(".jsp");
+ LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(delegate);
+ resolver.setEnableFallback(true);
+ resolver.setMobilePrefix("mobile/");
+ resolver.setTabletPrefix("tablet/");
+ return resolver;
+ }
+
+}
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+
+ <!-- Appenders -->
+ <appender name="console" class="org.apache.log4j.ConsoleAppender">
+ <param name="Target" value="System.out" />
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%-5p: %c - %m%n" />
+ </layout>
+ </appender>
+
+ <!-- Application Loggers -->
+ <logger name="org.springframework.showcases.lite">
+ <level value="info" />
+ </logger>
+
+ <!-- 3rdparty Loggers -->
+ <logger name="org.springframework.core">
+ <level value="info" />
+ </logger>
+
+ <logger name="org.springframework.beans">
+ <level value="info" />
+ </logger>
+
+ <logger name="org.springframework.context">
+ <level value="info" />
+ </logger>
+
+ <logger name="org.springframework.web">
+ <level value="info" />
+ </logger>
+
+ <!-- Root Logger -->
+ <root>
+ <priority value="info" />
+ <appender-ref ref="console" />
+ </root>
+
+</log4j:configuration>
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Class-Path:
+
@@ -0,0 +1,23 @@
+<%@ page session="false" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Spring Mobile | Lite Device Aware View Resolver</title>
+ </head>
+ <body>
+ <header>
+ <h1>About (Normal Site)</h1>
+ </header>
+ <p>Return to the <a href="<c:url value="/" />">Home</a> page.</p>
+ <footer>
+ An Apache-licensed technology showcase by SpringSource.
+ </footer>
+ <aside id="site-preference">
+ <p>Site:
+ <c:if test="${currentSitePreference.mobile}"><a href="?site_preference=normal">Normal</a> | Mobile | <a href="?site_preference=tablet">Tablet</a></c:if>
+ <c:if test="${currentSitePreference.tablet}"><a href="?site_preference=normal">Normal</a> | <a href="?site_preference=mobile">Mobile</a> | Tablet</c:if>
+ <c:if test="${currentSitePreference.normal}">Normal | <a href="?site_preference=mobile">Mobile</a> | <a href="?site_preference=tablet">Tablet</a></c:if>
+ </aside>
+ </body>
+</html>
@@ -0,0 +1,23 @@
+<%@ page session="false" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Spring Mobile | Lite Device Aware View Resolver</title>
+ </head>
+ <body>
+ <header>
+ <h1>Home (Normal Site)</h1>
+ </header>
+ <p>View the <a href="<c:url value="/about" />">About</a> page.</p>
+ <footer>
+ An Apache-licensed technology showcase by SpringSource.
+ </footer>
+ <aside id="site-preference">
+ <p>Site:
+ <c:if test="${currentSitePreference.mobile}"><a href="?site_preference=normal">Normal</a> | Mobile | <a href="?site_preference=tablet">Tablet</a></c:if>
+ <c:if test="${currentSitePreference.tablet}"><a href="?site_preference=normal">Normal</a> | <a href="?site_preference=mobile">Mobile</a> | Tablet</c:if>
+ <c:if test="${currentSitePreference.normal}">Normal | <a href="?site_preference=mobile">Mobile</a> | <a href="?site_preference=tablet">Tablet</a></c:if>
+ </aside>
+ </body>
+</html>
@@ -0,0 +1,24 @@
+<%@ page session="false" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Spring Mobile | Lite Device Aware View Resolver</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
+ </head>
+ <body>
+ <header>
+ <h1>About (Mobile Site)</h1>
+ </header>
+ <p>Return to the <a href="<c:url value="/" />">Home</a> page.</p>
+ <footer>
+ An Apache-licensed technology showcase by SpringSource.
+ </footer>
+ <aside id="site-preference">
+ <p>Site:
+ <c:if test="${currentSitePreference.mobile}"><a href="?site_preference=normal">Normal</a> | Mobile | <a href="?site_preference=tablet">Tablet</a></c:if>
+ <c:if test="${currentSitePreference.tablet}"><a href="?site_preference=normal">Normal</a> | <a href="?site_preference=mobile">Mobile</a> | Tablet</c:if>
+ <c:if test="${currentSitePreference.normal}">Normal | <a href="?site_preference=mobile">Mobile</a> | <a href="?site_preference=tablet">Tablet</a></c:if>
+ </aside>
+ </body>
+</html>
@@ -0,0 +1,24 @@
+<%@ page session="false" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Spring Mobile | Lite Device Aware View Resolver</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
+ </head>
+ <body>
+ <header>
+ <h1>Home (Mobile Site)</h1>
+ </header>
+ <p>View the <a href="<c:url value="/about" />">About</a> page.</p>
+ <footer>
+ An Apache-licensed technology showcase by SpringSource.
+ </footer>
+ <aside id="site-preference">
+ <p>Site:
+ <c:if test="${currentSitePreference.mobile}"><a href="?site_preference=normal">Normal</a> | Mobile | <a href="?site_preference=tablet">Tablet</a></c:if>
+ <c:if test="${currentSitePreference.tablet}"><a href="?site_preference=normal">Normal</a> | <a href="?site_preference=mobile">Mobile</a> | Tablet</c:if>
+ <c:if test="${currentSitePreference.normal}">Normal | <a href="?site_preference=mobile">Mobile</a> | <a href="?site_preference=tablet">Tablet</a></c:if>
+ </aside>
+ </body>
+</html>
Oops, something went wrong.

0 comments on commit 5fbaf55

Please sign in to comment.