Permalink
Browse files

Updates to lite-site-preference-handler sample app

  • Loading branch information...
1 parent 97b87cc commit 788c90509c02e9c24d6d09e0cf925523c791bc50 @royclarkson royclarkson committed Oct 30, 2012
@@ -33,14 +33,17 @@
@RequestMapping("/")
public String home(SitePreference sitePreference, Model model) {
- if (sitePreference == SitePreference.MOBILE) {
+ if (sitePreference == SitePreference.NORMAL) {
+ logger.info("Site preference is normal");
+ return "home";
+ } else if (sitePreference == SitePreference.MOBILE) {
logger.info("Site preference is mobile");
return "home-mobile";
} else if (sitePreference == SitePreference.TABLET) {
logger.info("Site preference is tablet");
return "home-tablet";
} else {
- logger.info("Site preference is normal, or no site preference selected");
+ logger.info("no site preference");
return "home";
}
}
@@ -22,7 +22,7 @@
* @author Roy Clarkson
*/
@Configuration
-@ComponentScan(basePackages="org.springframework.showcases.lite")
+@ComponentScan(basePackages = "org.springframework.showcases.lite")
public class ComponentConfig {
}
@@ -19,6 +19,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver;
+import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;
+import org.springframework.mobile.device.DeviceWebArgumentResolver;
import org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor;
import org.springframework.mobile.device.site.SitePreferenceHandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
@@ -40,10 +43,25 @@
// implementing WebMvcConfigurer
public void addInterceptors(InterceptorRegistry registry) {
+
+ /*
+ * Include the handler for device resolution. Strictly speaking, a device resolver is not required for site
+ * preference management. However for this sample application, it's useful to see what device is being resolved.
+ */
+ //registry.addInterceptor(new DeviceResolverHandlerInterceptor());
+
+ /*
+ * Include the handler for site preference management. This handler defaults to using cookie based storage.
+ */
registry.addInterceptor(new SitePreferenceHandlerInterceptor());
}
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
+
+ // argument resolver that supports accessing the current device from the web request
+ //argumentResolvers.add(new DeviceHandlerMethodArgumentResolver());
+
+ // argument resolver that supports accessing the current site preference from the web request
argumentResolvers.add(new SitePreferenceHandlerMethodArgumentResolver());
}
@@ -1,24 +1,14 @@
<%@ page session="false" %>
-<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
- <title>Spring Mobile | Lite Showcase</title>
+ <title>Spring Mobile | Lite Site Preference Handler</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>
<body>
- <header>
- <h1>Mobile Site</h1>
- </header>
- <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>
- </p>
- </aside>
+ <%@include file="includes/header.jsp" %>
+ <%@include file="includes/content.jsp" %>
+ <%@include file="includes/footer.jsp" %>
+ <%@include file="includes/menu.jsp" %>
</body>
</html>
@@ -1,24 +1,14 @@
<%@ page session="false" %>
-<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
- <title>Spring Mobile | Lite Showcase</title>
+ <title>Spring Mobile | Lite Site Preference Handler</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>
<body>
- <header>
- <h1>Tablet Site</h1>
- </header>
- <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>
- </p>
- </aside>
+ <%@include file="includes/header.jsp" %>
+ <%@include file="includes/content.jsp" %>
+ <%@include file="includes/footer.jsp" %>
+ <%@include file="includes/menu.jsp" %>
</body>
</html>
@@ -1,24 +1,14 @@
<%@ page session="false" %>
-<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
- <title>Spring Mobile | Lite Showcase</title>
+ <title>Spring Mobile | Lite Site Preference Handler</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>
<body>
- <header>
- <h1>Normal Site</h1>
- </header>
- <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>
- </p>
- </aside>
+ <%@include file="includes/header.jsp" %>
+ <%@include file="includes/content.jsp" %>
+ <%@include file="includes/footer.jsp" %>
+ <%@include file="includes/menu.jsp" %>
</body>
</html>
@@ -0,0 +1,66 @@
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
+<style>
+table{
+ margin:30px;
+}
+
+table{
+ border-collapse: seperate;
+ border-spacing: 0px;
+ border: 1px solid gray;
+}
+
+td{
+ border: 1px solid gray;
+ padding: 6px;
+}
+
+thead{
+ font-weight: bold;
+}
+
+tbody{
+ text-align: center;
+}
+</style>
+<table>
+ <thead>
+ <tr><td>Current Site Preference</td><td>Current Device</td></tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>
+ <c:choose>
+ <c:when test="${currentSitePreference.normal}">
+ Normal
+ </c:when>
+ <c:when test="${currentSitePreference.mobile}">
+ Mobile
+ </c:when>
+ <c:when test="${currentSitePreference.tablet}">
+ Tablet
+ </c:when>
+ <c:otherwise>
+ None
+ </c:otherwise>
+ </c:choose>
+ </td>
+ <td>
+ <c:choose>
+ <c:when test="${currentDevice.normal}">
+ Normal
+ </c:when>
+ <c:when test="${currentDevice.mobile}">
+ Mobile
+ </c:when>
+ <c:when test="${currentDevice.tablet}">
+ Tablet
+ </c:when>
+ <c:otherwise>
+ None
+ </c:otherwise>
+ </c:choose>
+ </td>
+ </tr>
+ </tbody>
+</table>
@@ -0,0 +1,3 @@
+<footer>
+ An Apache-licensed technology showcase by SpringSource.
+</footer>
@@ -0,0 +1,19 @@
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
+<header>
+ <h1>
+ <c:choose>
+ <c:when test="${currentSitePreference.normal}">
+ Normal Site
+ </c:when>
+ <c:when test="${currentSitePreference.mobile}">
+ Mobile Site
+ </c:when>
+ <c:when test="${currentSitePreference.tablet}">
+ Tablet Site
+ </c:when>
+ <c:otherwise>
+ Default Site (no site preference)
+ </c:otherwise>
+ </c:choose>
+ </h1>
+</header>
@@ -0,0 +1,31 @@
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
+<aside id="site-preference">
+ <p>
+ <c:choose>
+ <c:when test="${currentSitePreference.normal}">
+ <a href="<c:url value="/"/>">Default</a> |
+ Normal |
+ <a href="?site_preference=mobile">Mobile</a> |
+ <a href="?site_preference=tablet">Tablet</a>
+ </c:when>
+ <c:when test="${currentSitePreference.mobile}">
+ <a href="<c:url value="/"/>">Default</a> |
+ <a href="?site_preference=normal">Normal</a> |
+ Mobile |
+ <a href="?site_preference=tablet">Tablet</a>
+ </c:when>
+ <c:when test="${currentSitePreference.tablet}">
+ <a href="<c:url value="/"/>">Default</a> |
+ <a href="?site_preference=normal">Normal</a> |
+ <a href="?site_preference=mobile">Mobile</a> |
+ Tablet
+ </c:when>
+ <c:otherwise>
+ Default |
+ <a href="?site_preference=normal">Normal</a> |
+ <a href="?site_preference=mobile">Mobile</a> |
+ <a href="?site_preference=tablet">Tablet</a>
+ </c:otherwise>
+ </c:choose>
+ </p>
+</aside>
@@ -10,7 +10,7 @@
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
- <!-- Location of Java @Configuration classes that configure the components
+ <!-- Location of Java @Configuration classes that configure the components
that makeup this application -->
<context-param>
<param-name>contextConfigLocation</param-name>
@@ -12,6 +12,12 @@
private HomeController controller = new HomeController();
@Test
+ public void homePageDefaultDevice() {
+ Model model = new ExtendedModelMap();
+ assertEquals("home", controller.home(null, model));
+ }
+
+ @Test
public void homePageNormalDevice() {
Model model = new ExtendedModelMap();
assertEquals("home", controller.home(SitePreference.NORMAL, model));

0 comments on commit 788c905

Please sign in to comment.