本文使用 Spring Web model-view-controller (MVC) framework
建立一個簡單的一個Hello World 程式 且可判斷Mobile Tablet Desktop進入這頁面
有三種方式:
方式二:網站首選項管理
使用者可以透過網站偏好管理指出他現在的網址是透過移動式設備或者是電腦桌面進入這網頁
1、請參考建立一個SpringMVC Annotation Project - Hello World此篇文章,以下是以此作為延伸
2、加入 spring-mobile-device.jar
加入在Maven專案中的pom.xml
請參考建立一個SpringMVC Annotation Project 2-1 (判斷Mobile Tablet Desktop) - Hello World
3. 更改Controller 部分
package com.sample.web.controller; import org.springframework.mobile.device.site.SitePreference; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET) public ModelAndView welcomePage(SitePreference sitePreference) { ModelAndView model = new ModelAndView(); if (sitePreference == SitePreference.NORMAL) { model.setViewName("home"); } else if (sitePreference == SitePreference.MOBILE) { model.setViewName("home-mobile"); } else if (sitePreference == SitePreference.TABLET) { model.setViewName("home-tablet"); } else { model.setViewName("home"); } return model; } }
4. 更改AppConfig 部分(Spring 設定) - 需繼承 WebMvcConfigurerAdapter
目錄 : src/main/java/com/sample/config
檔名 : AppConfig.java
package com.sample.config; import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; 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.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @EnableWebMvc @Configuration @ComponentScan({ "com.sample.web.*" }) public class AppConfig extends WebMvcConfigurerAdapter{ /** * 設備攔截器 * @return DeviceResolverHandlerInterceptor */ @Bean public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() { return new DeviceResolverHandlerInterceptor(); } /** * 網站偏好攔截器 * @return SitePreferenceHandlerInterceptor */ @Bean public SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor() { return new SitePreferenceHandlerInterceptor(); } /** * 網站偏好處理程序 * @return SitePreferenceHandlerMethodArgumentResolver */ @Bean public SitePreferenceHandlerMethodArgumentResolver sitePreferenceHandlerMethodArgumentResolver() { return new SitePreferenceHandlerMethodArgumentResolver(); } /** * 加入SPRING攔截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(deviceResolverHandlerInterceptor()); registry.addInterceptor(sitePreferenceHandlerInterceptor()); } /** * 加入參數解析器 */ @Override public void addArgumentResolvers(ListargumentResolvers) { argumentResolvers.add(sitePreferenceHandlerMethodArgumentResolver()); } /** * 頁面導向 * @return LiteDeviceDelegatingViewResolver */ @Bean public LiteDeviceDelegatingViewResolver liteDeviceAwareViewResolver() { InternalResourceViewResolver delegate = new InternalResourceViewResolver(); delegate.setPrefix("/WEB-INF/pages/"); delegate.setSuffix(".jsp"); LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(delegate); resolver.setMobilePrefix("mobile/"); resolver.setTabletPrefix("tablet/"); return resolver; } }
請參考建立一個SpringMVC Annotation Project 2-1 (判斷Mobile Tablet Desktop) - Hello World
6.JSP部分:
(A). 放置結構:
(B). home.jsp :
<html>
<body>
<h1>SpringMVC - Annotation : Hello World Deskop</h1>
</body>
</html>
(C).home-mobile.jsp
<html>
<body>
<h1>SpringMVC - Annotation : Hello World Mobile</h1>
</body>
</html>
(D).home-tablet.jsp
<html>
<body>
<h1>SpringMVC - Annotation : Hello World Tablet</h1>
</body>
</html>
呈現結果:
結果-電腦
結果-手機
其它文章
沒有留言:
張貼留言