2012-08-21

建立 Google App Engine + Spring MVC + Tiles + Maven + Eclipse 專案

  • 建立專案
    • 使用 Eclipse -> New Web Application Project
      • 輸入 Project name 與 Package
      • 取消勾選 Use Google Web Toolkit
      • 確認勾選 Google App Engine
  • 加入 Maven
    • 於專案目錄下建立 pom.xml
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>idv.neil.vocabulary</groupId>
          <artifactId>vocabulary</artifactId>
          <packaging>war</packaging>
          <version>1.0-SNAPSHOT</version>
          <name>Vocabulary Maven Webapp</name>
          <url>http://maven.apache.org</url>
          <dependencies>
              <!-- Spring MVC -->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-webmvc</artifactId>
                  <version>3.0.5.RELEASE</version>
                  <scope>runtime</scope>
                  <exclusions>
                      <exclusion>
                          <groupId>commons-logging</groupId>
                          <artifactId>commons-logging</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
              <!-- JSTL -->
              <dependency>
                  <groupId>javax.servlet</groupId>
                  <artifactId>jstl</artifactId>
                  <version>1.2</version>
              </dependency>
              <!-- Tiles -->
              <dependency>
                  <groupId>org.apache.tiles</groupId>
                  <artifactId>tiles-jsp</artifactId>
                  <version>2.2.2</version>
              </dependency>
              <!-- SLF4J -->
              <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>jcl-over-slf4j</artifactId>
                  <version>1.6.1</version>
                  <scope>runtime</scope>
              </dependency>
              <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>slf4j-api</artifactId>
                  <version>1.6.1</version>
                  <scope>runtime</scope>
              </dependency>
              <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>slf4j-jdk14</artifactId>
                  <version>1.6.1</version>
                  <scope>runtime</scope>
              </dependency>
              <!-- Commons -->
              <dependency>
                  <groupId>commons-lang</groupId>
                  <artifactId>commons-lang</artifactId>
                  <version>2.6</version>
              </dependency>
              <!-- Spring JSON -->
              <dependency>
                  <groupId>org.codehaus.jackson</groupId>
                  <artifactId>jackson-mapper-asl</artifactId>
                  <version>1.7.3</version>
                  <type>jar</type>
                  <scope>runtime</scope>
              </dependency>
              <!-- Spring Validation -->
              <dependency>
                  <groupId>javax.validation</groupId>
                  <artifactId>validation-api</artifactId>
                  <version>1.0.0.GA</version>
              </dependency>
              <dependency>
                  <groupId>org.hibernate</groupId>
                  <artifactId>hibernate-validator</artifactId>
                  <version>4.0.2.GA</version>
              </dependency>
          </dependencies>
          <build>
              <finalName>vocabulary</finalName>
              <sourceDirectory>src</sourceDirectory>
              <resources>
                  <resource>
                      <directory>src</directory>
                      <includes>
                          <include>**/*.xml</include>
                          <include>**/*.properties</include>
                      </includes>
                  </resource>
              </resources>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-war-plugin</artifactId>
                      <configuration>
                          <warSourceDirectory>war</warSourceDirectory>
                          <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
                      </configuration>
                  </plugin>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-resources-plugin</artifactId>
                      <configuration>
                          <encoding>MS950</encoding>
                      </configuration>
                  </plugin>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <configuration>
                          <includes>
                              <include>src/**</include>
                          </includes>
                      </configuration>
                  </plugin>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-eclipse-plugin</artifactId>
                      <configuration>
                          <buildOutputDirectory>war/WEB-INF/classes</buildOutputDirectory>
                          <downloadSources>true</downloadSources>
                          <downloadJavadocs>true</downloadJavadocs>
                          <classpathContainers>
                              <classpathContainer>com.google.appengine.eclipse.core.GAE_CONTAINER</classpathContainer>
                              <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
                          </classpathContainers>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      </project>
    • 於專案根目錄下執行以下指令,產生 Eclipse 專用檔案。
      mvn eclipse:eclipse
    • 於專案根目錄下執行以下指令,產生必要的 library。
      mvn package
    • 複製 target\vocabulary\WEB-INF\lib 下所有 library 到 war\WEB-INF\lib,不用覆蓋原有的 library。
    • 回到 Eclipse,重新整理專案。
  • 修改 web.xml
    <?xml version="1.0" encoding="utf-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
        <!-- Reads request input using UTF-8 encoding -->
        <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!-- config Spring app ctx -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/vocabulary-appctx.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- config Spring web ctx -->
        <servlet>
            <servlet-name>vocabulary</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/vocabulary-webctx.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet>
            <servlet-name>Initail</servlet-name>
            <servlet-class>idv.neil.vocabulary.web.Initial</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>vocabulary</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
  • 建立 Initial.java
    package idv.neil.vocabulary.web;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    
    import com.google.appengine.api.users.UserService;
    import com.google.appengine.api.users.UserServiceFactory;
    
    @SuppressWarnings("serial")
    public class Initial extends HttpServlet {
    
        private static final String G_LOGOUT_URL = "G_LOGOUT_URL";
    
        @Override
        public void init() throws ServletException {
            UserService gus = UserServiceFactory.getUserService();
            this.getServletContext().setAttribute(G_LOGOUT_URL, gus.createLogoutURL("/"));
        }
    
    }
  • 建立 /war/WEB-INF/vocabulary-appctx.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        <context:component-scan base-package="idv.neil.vocabulary.service,idv.neil.vocabulary.dao,idv.neil.vocabulary.model"/>
        
    </beans>
  • 建立 /war/WEB-INF/vocabulary-webctx.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        <context:component-scan base-package="idv.neil.vocabulary.web.ctrl"/>
        
        <mvc:annotation-driven/>
        
        <mvc:interceptors>
            <bean class="idv.neil.vocabulary.web.AuthenticationInterceptor"/>
        </mvc:interceptors>
        
        <bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver"/>
        
        <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
            <property name="definitions">
                <value>/WEB-INF/tiles.xml</value>
            </property>
        </bean>
        
        <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>
        
    </beans>
  • 建立 /war/index.jsp
    <%@ page contentType="text/html; charset=UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <c:redirect url="/index.do" />
  • 建立 IndexController.java
    package idv.neil.vocabulary.web.ctrl;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class IndexController {
    
        private static final Logger log = LoggerFactory.getLogger(IndexController.class);
    
        @RequestMapping("/index")
        public ModelAndView index() {
            ModelAndView mav = new ModelAndView("index");
            return mav;
        }
    }
  • 建立 /war/WEB-INF/tiles.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE tiles-definitions PUBLIC
        "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
        "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
        
    <tiles-definitions>
        
        <definition name="index" template="/WEB-INF/jsp/layout/layout.jsp">
            <put-attribute name="header" value="/WEB-INF/jsp/layout/header.jsp"/>
            <put-attribute name="menu" value="/WEB-INF/jsp/layout/menu.jsp"/>
            <put-attribute name="content" value="/WEB-INF/jsp/layout/content.jsp"/>
            <put-attribute name="footer" value="/WEB-INF/jsp/layout/footer.jsp"/>
        </definition>
        
    </tiles-definitions>
  • 建立 style/layout.css

沒有留言:

張貼留言