本文使用 Spring Web model-view-controller (MVC) framework
建立一個簡單的一個Hello World 程式 且可判斷Mobile Tablet Desktop進入這頁面
有三種方式:
方式一:設備檢測
設備檢測是分析HTTP请求以决定發出请求的設備的過程。
這通常是通過檢測User-Agent Header和其他请求来實現作的。
1、請參考建立一個SpringMVC Annotation Project - Hello World此篇文章,以下是以此作為延伸
2、加入 spring-mobile-device.jar
加入在Maven專案中的pom.xml
<dependency>
<groupId>org.springframework.mobile</groupId>
<artifactId>spring-mobile-device</artifactId>
<version>1.1.3.RELEASE</version>
</dependency>
3. 更改Controller 部分
package com.sample.web.controller; import org.springframework.mobile.device.Device; 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(Device device) { ModelAndView model = new ModelAndView(); if( device.isMobile() ){ model.addObject("message", "Hello World Mobile User!"); }else if( device.isTablet() ){ model.addObject("message", "Hello World Tablet User!"); }else if( device.isNormal() ){ model.addObject("message", "Hello World Desktop User!"); } model.setViewName("hello"); 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.DeviceHandlerMethodArgumentResolver; import org.springframework.mobile.device.DeviceResolverHandlerInterceptor; 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; import org.springframework.web.servlet.view.JstlView; @EnableWebMvc @Configuration @ComponentScan({ "com.sample.web.*" }) public class AppConfig extends WebMvcConfigurerAdapter{ /** * 設備攔截器 * @return DeviceResolverHandlerInterceptor */ @Bean public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() { return new DeviceResolverHandlerInterceptor(); } /** * 設備處理程序 * @return DeviceHandlerMethodArgumentResolver */ @Bean public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() { return new DeviceHandlerMethodArgumentResolver(); } /** * 加入SPRING攔截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(deviceResolverHandlerInterceptor()); } /** * 加入參數解析器 */ @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver>argumentResolvers) { argumentResolvers.add(deviceHandlerMethodArgumentResolver()); } @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/pages/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
5.建立 SpringMvcInitializer.java(增加CharacterEncodingFilter)
目錄 : src/main/java/com/sample/config/core
檔名 : SpringMvcInitializer.java(以下兩張圖片web.xml和Annotation寫法比對)
程式碼:
package com.sample.config.core; import javax.servlet.Filter; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import com.sample.config.AppConfig; public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { return new Class[] { AppConfig.class }; } @Override protected Class[] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } @Override protected Filter[] getServletFilters() { CharacterEncodingFilter utf8Filter = new CharacterEncodingFilter(); utf8Filter.setEncoding("UTF-8"); utf8Filter.setForceEncoding(true); return new Filter[] { utf8Filter }; } }6.執行成功畫面:
URL = http://127.0.0.1:8080/SampleSpringMVC-2/
電腦部分:
手機執行畫面:
下一篇:
其它文章
沒有留言:
張貼留言