本文使用 Spring Web model-view-controller (MVC) framework
建立一個簡單的一個Hello World 程式 且可判斷Mobile Tablet Desktop進入這頁面
有三種方式:
方式三:站點切換
指的是可以根據方式一以及方式二將使用者切換到適合網站的能力
此方式有四種方法使用(mDot, dotMobi, standard以及urlPath)
本文使用urlPath呈現效果
1、請參考建立一個SpringMVC Annotation Project - Hello World此篇文章,以下是以此作為延伸
2、加入 spring-mobile-device.jar
3. Controller 部分
請參考建立一個SpringMVC Annotation Project 2-1 (判斷Mobile Tablet Desktop) - Hello World
4. 更改AppConfig 部分(Spring 設定) - 需繼承 WebMvcConfigurerAdapter
目錄 : src/main/java/com/sample/config
檔名 : AppConfig.java
package com.sample.config; 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.switcher.SiteSwitcherHandlerInterceptor; 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(); } /** * 網址切換攔截器 * 第一個参数用於指定mobile的路徑,第二个参数用於指定應用的root path * @return SiteSwitcherHandlerInterceptor */ @Bean public SiteSwitcherHandlerInterceptor siteSwitcherHandlerInterceptor() { return SiteSwitcherHandlerInterceptor.urlPath("/m", "/SampleSpringMVC-2-3"); } /** * 加入SPRING攔截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(deviceResolverHandlerInterceptor()); registry.addInterceptor(siteSwitcherHandlerInterceptor()); } /** * 頁面導向 * @return InternalResourceViewResolver */ @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(需要增加urlPattern)
目錄 : src/main/java/com/sample/config/code
檔名 : SpringMvcInitializer.java
package com.sample.config.core; import javax.servlet.Filter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration.Dynamic; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.DispatcherServlet; 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 }; } @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); rootContext.setServletContext(servletContext); rootContext.scan("com.sample.config"); Dynamic dynamic = servletContext.addServlet("mvc-dispatcher", new DispatcherServlet(rootContext)); dynamic.addMapping("/","/m/*"); dynamic.setLoadOnStartup(1); servletContext.addListener(new ContextLoaderListener(rootContext)); } }
6.建立JSP 頁面
目錄 : src/main/webapp/WEB-INF/pages/
檔名 : hello.jsp
程式碼:
<html>
<body>
<h1>SpringMVC - Annotation :${message}</h1>
</body>
</html>
7.執行成功畫面:
URL = http://127.0.0.1:8081/SampleSpringMVC-2-3/
下一篇:
[JAVA_Spring]建立一個SpringMVC Annotation Project 2-3 (判斷Mobile Tablet Desktop) - Hello World
其它文章
沒有留言:
張貼留言