Permalink
Browse files

Updates to lite-device-resolver sample app

  • Loading branch information...
1 parent 788c905 commit e5e124b308a62a1012c4f231e0cec5fcd95e88a6 @royclarkson royclarkson committed Oct 30, 2012
View
31 lite-device-resolver/src/main/java/org/springframework/showcases/lite/HomeController.java
@@ -23,27 +23,26 @@
import org.springframework.web.bind.annotation.RequestMapping;
/**
- * Handles requests for the showcase application.
+ * Handles requests for the sample application.
* @author Roy Clarkson
*/
@Controller
public class HomeController {
-
+
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
-
+
@RequestMapping("/")
- public String home(Device device, Model model) {
- if (device.isMobile()) {
- logger.info("Hello mobile user!");
- model.addAttribute("deviceType", "mobile");
- } else if (device.isTablet()) {
- logger.info("Hello tablet user!");
- model.addAttribute("deviceType", "tablet");
- } else {
- logger.info("Hello desktop user!");
- model.addAttribute("deviceType", "normal");
- }
- return "home";
- }
+ public String home(Device device, Model model) {
+ if (device == null) {
+ logger.info("no device detected");
+ } else if (device.isNormal()) {
+ logger.info("Device is normal");
+ } else if (device.isMobile()) {
+ logger.info("Device is mobile");
+ } else if (device.isTablet()) {
+ logger.info("Device is tablet");
+ }
+ return "home";
+ }
}
View
2 ...ice-resolver/src/main/java/org/springframework/showcases/lite/config/ComponentConfig.java
@@ -22,7 +22,7 @@
* @author Roy Clarkson
*/
@Configuration
-@ComponentScan(basePackages="org.springframework.showcases.lite")
+@ComponentScan(basePackages = "org.springframework.showcases.lite")
public class ComponentConfig {
}
View
4 lite-device-resolver/src/main/java/org/springframework/showcases/lite/config/WebConfig.java
@@ -50,9 +50,9 @@ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
-
+
// additional webmvc-related beans
-
+
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
View
13 lite-device-resolver/src/main/webapp/WEB-INF/views/home.jsp
@@ -3,16 +3,19 @@
<!DOCTYPE html>
<html>
<head>
- <title>Spring Mobile | Lite Showcase</title>
+ <title>Spring Mobile | Lite Device Resolver</title>
</head>
<body>
<header>
<h1>Device Resolver Example</h1>
</header>
- <p>
- <c:if test="${currentDevice.mobile}">Hello mobile user!</c:if>
- <c:if test="${currentDevice.tablet}">Hello tablet user!</c:if>
- <c:if test="${currentDevice.normal}">Hello desktop user!</c:if>
+ <p>Device:
+ <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>Default</c:otherwise>
+ </c:choose>
</p>
<footer>
An Apache-licensed technology showcase by SpringSource.
View
18 ...-device-resolver/src/test/java/org/springframework/showcases/lite/HomeControllerTest.java
@@ -2,8 +2,6 @@
import static org.junit.Assert.assertEquals;
-import java.util.Map;
-
import org.junit.Test;
import org.springframework.mobile.device.DeviceType;
import org.springframework.ui.ExtendedModelMap;
@@ -14,27 +12,27 @@
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(new StubDevice(DeviceType.NORMAL), model));
- Map<String,Object> attributes = model.asMap();
- assertEquals(attributes.get("deviceType"), "normal");
}
-
+
@Test
public void homePageMobileDevice() {
Model model = new ExtendedModelMap();
assertEquals("home", controller.home(new StubDevice(DeviceType.MOBILE), model));
- Map<String,Object> attributes = model.asMap();
- assertEquals(attributes.get("deviceType"), "mobile");
}
-
+
@Test
public void homePageTabletDevice() {
Model model = new ExtendedModelMap();
assertEquals("home", controller.home(new StubDevice(DeviceType.TABLET), model));
- Map<String,Object> attributes = model.asMap();
- assertEquals(attributes.get("deviceType"), "tablet");
}
}

0 comments on commit e5e124b

Please sign in to comment.