2014年12月11日 星期四

建立一個SpringMVC Annotation Project 2-3 (判斷Mobile Tablet Desktop) - Hello World



本文使用 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
















其它文章

沒有留言:

張貼留言

標籤

Oracle (150) Oracle DB (144) Oracle_DB (143) Oracle SQL (135) JAVA (84) css-基本類 (65) MySQL (59) CSS Selector (58) jQuery (49) JavaScript-基本類 (39) Spring Boot (38) JavaScript (37) JavaScript HTML DOM (37) JavaScript-HTML_DOM (36) CSS3 (30) JAVA-基本類 (28) jQuery UI (27) Apache (23) Oracle GROUP BY (20) datepicker (20) Android (18) Oracle Date (17) c (17) JAVA-lang套件 (16) Linux (16) Oracle Sub Query (16) Spring-基本類 (16) jQuery-基本類 (16) MySQL-進階系列教學 (15) Android基本類 (14) Grails (14) Oracle join (14) SQLite (13) Spring (13) WIN7-基本類 (13) grails-基本類 (13) linux cent os (13) CKEditor (12) JAVA-流程控制類 (12) JAVA_Spring (12) PHP (11) Spring MVC (11) MySQL-基本系列教學 (10) Notepad (10) Notepad++ (10) SQLite for java (10) Windows (10) c/c++ (10) eclipse (9) jQuery-Selector (9) sqldeveloper (9) DB_Toad (8) JAVA_IDE_Eclipse (8) JavaScript-String類 (8) MySQL DB Toad (8) MySQL-DATE相關 (8) MySQL-函式相關 (8) Spring Bean (8) Android Studio (7) HTML5 (7) Hibernate (7) JAVA-OCWCD (7) JavaScript-陣列類 (7) Docker (6) JAVA-程式分享 (6) JAVA.util套件 (6) JavaScript-數學類 (6) MinGw (6) MySQL-其它類 (6) Servlet (6) centos (6) Apache_Tomcat (5) Apache套件_POI (5) CSS (5) JavaScript-Date物件 (5) JavaScript-其它類 (5) PostgreSQL (5) httpd (5) log4j (5) 基本資訊 (5) 開發工具 (5) CSS Properties (4) Dev-C++ (4) IntelliJ IDEA (4) Oracle DDL (4) Sublime (4) TortoiseSVN (4) apache_Maven (4) Android NDK (3) Eclipse IDE for C/C++ (3) Hibernate-基本類 (3) JAVA-問題 (3) JAVA-綀習分享 (3) JVM (3) Linux 指令 (3) Proxy Server (3) Spring Mobile (3) Spring web (3) Squid (3) VirtualBox (3) maven (3) zk (3) 生活其它 (3) Bootstrap (2) Filter (2) JAVA_IO (2) JAVA_其它_itext套件 (2) JBoss-問題 (2) JSP (2) Jboss (2) Listener (2) MySQL-語法快速查詢 (2) Spring AOP (2) Spring Batch (2) Spring Boot Actuator (2) Spring i18n (2) Subversive (2) Tomcat 8 (2) UML (2) WebJars (2) WinMerge (2) c++ (2) c語言綀習題 (2) jQuery Mobile (2) jQuery-事件處理 (2) jQuery-套件類 (2) putty (2) svn (2) weblogic (2) Apache_JMeter (1) Apache套件_BeanUtils (1) Apache套件_StringUtils (1) Base64 (1) Google API (1) HTML5-基本類 (1) Heap (1) JAVA 7 (1) JAVA SE 、JAVA EE、JAVA ME (1) JAVA 日期 (1) JAVA-OCJP (1) JAVA-WEB (1) JAVA_IDE (1) JAVA其它 (1) JBoss Server (1) JDK (1) JMX (1) JRE (1) Java RMI (1) Java String (1) Joda Time (1) Linux_其它 (1) MySQL教學 (1) Oracle_VirtualBox (1) SQL Server (1) SWT (1) Session (1) Stack (1) Struts 2 (1) Tool (1) ZK Studio (1) csv (1) grails-其它類 (1) jQuery-進階 (1) java mail (1) java web (1) java8 (1) jsoup (1) mockmvc (1) modules (1) tomcat (1) win10 (1) 其它類 (1) 圖片工具 (1) 模擬器 (1) 讀書分享 (1) 開發資訊 (1)

精選文章

初學 Java 的 HelloWorld 程式

撰寫一個JAVA程式 public class HelloWorld{ public static void main(String[ ] args){ System.out.println("我第一支Java程式!!"); } } ...