close

開發環境

1.Eclipse 4.4.2

2.JDK 1.8

3.Eclipse plugin - m2e-wtp - Maven Integration for WTP 1.1.0

4.Eclipse plugin - Spring IDE 3.6.4

5.Eclipse plugin - JDT patch with JAVA8 1.0.0

6.Spring Framework 3.2.4

7.Tomcat 8.0

 

一. 下載Eclipse

(1)在Win7-64bits要下載相對應的版本

Image 1  

(2)到本機解壓縮後,即可開啟C:\eclipse\eclipse.exe

Image 6

 

(3)從 eclipse.ini 可設定開啟Eclipse使用的參數:-Xms256m表示最小以256MB開啟Eclipse,-Xmx512m表示Eclipse最大記憶體使用量512MB

 Image 7  

二. 下載JDK

Image 2  

三.下載Eclipse plugin:

 (1) 選單 Eclipse> Market Place

 安裝Maven插件

Image 11  

 安裝Spring IDE插件

Image 9  

 安裝支援JAVA8的JDT插件

Image 12  

 

四. 新增Spring專案

(1) 選單  File > New > Other

Image 13  

(2) 輸入Project name、選擇Templates

Image 14  

(3)最上層package name

Image 15  

(4)新增/MyMVC/src/main/java/com/mytest/mvc/HomeController.java

tips1 : 使用@Controller標註這個類別是controller。

tips2 : 在function上方使用@RequestMapping,value = "/"代表程式的啟始根目錄,在本範例中

   的根目錄就在http://localhost:8080/mvc/,當request這個URL就會對應到home(..)這個

   function。

tips3 : 可利用Model物件的addAttribute方法,把要傳遞的值formattedDate帶到jsp頁面呈現。

tips4 : return "home" 返回 home.jsp 這個頁面。

tips5 : 第二個@RequestMapping,value = "/hello"代表

   request http://localhost:8080/mvc/hello 會對應到hello()這個function

package com.mytest.mvc;

 

import java.text.DateFormat;

import java.util.Date;

import java.util.Locale;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

 

@Controller

publicclass HomeController{

     privatestaticfinal Logger logger = LoggerFactory.getLogger(HomeController.class);

    

     @RequestMapping(value = "/", method = RequestMethod.GET)

     public String home(Locale locale, Model model) {

         logger.info("Welcome home! The client locale is {}."locale);        

         Date date = new Date();

         DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,                                                              DateFormat.LONGlocale);

        

         String formattedDate = dateFormat.format(date);

         logger.info(formattedDate);

         model.addAttribute("serverTime"formattedDate );

        

         return"home";

     }

    

     @RequestMapping(value = "/hello", method = RequestMethod.GET)

     public String hello(){

         return"home";

     }

}

(5)新增/MyMVC/src/main/webapp/WEB-INF/views/home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ page session="false" %>

<%@ page contentType="text/html; charset=UTF-8"%>

<html>

<head>

        <title>Home</title>

</head>

<body>

<h1>

        Hello world! 

</h1>

 

<P>  The time on the server is ${serverTime}. </P>

</body>

</html>

(6)修改/MyMVC/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- SpringMVC配置文件所需的xml  namespace  -->

<beans xmlns="http://www.springframework.org/schema/beans" 

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:cxf="http://cxf.apache.org/core"    

       xmlns:tx="http://www.springframework.org/schema/tx"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans.xsd

       http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd

       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

   

    <mvc:annotation-driven />

   

    <!-- 配置Controller要掃描的package路徑 -->

    <context:component-scan base-package="com.mytest.mvc" />

   

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->

     <mvc:resources mapping="/js/**" location="/resources/js/" />

    

    <!-- 配置view -->

    <bean id="jspViewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="viewClass"

        value="org.springframework.web.servlet.view.JstlView" />

        <!-- 解析器解析/WEB-INF/views/路徑下,以.jsp結尾的視圖文件 -->

        <property name="prefix" value="/WEB-INF/views/" />

        <property name="suffix" value=".jsp" />

    </bean> 

</beans>

(7)修改/MyMVC/src/main/webapp/WEB-INF/web.xml

其中這段表示所有 url request 從根目錄開始有包含 "/" 者都要分配給controller。

<servlet-mapping>

     <servlet-name>appServlet</servlet-name>

     <url-pattern>/</url-pattern>

</servlet-mapping>

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 

     <!-- Processes application requests -->

     <!-- 配置DispatcherServlet -->

     <servlet>

         <servlet-name>appServlet</servlet-name>

         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

         <init-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>

         </init-param>

         <load-on-startup>1</load-on-startup>

     </servlet>

    

     <servlet-mapping>

         <servlet-name>appServlet</servlet-name>

         <url-pattern>/</url-pattern>

     </servlet-mapping>

    

     <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->

    

     <context-param>

         <param-name>contextConfigLocation</param-name>

         <param-value>/WEB-INF/spring/root-context.xml</param-value>

     </context-param>

    

     <!-- Creates the Spring Container shared by all Servlets and Filters -->

     <!-- 配置上下文載入器 -->

     <listener>

         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

     </listener>

</web-app>

(8)修改/MyMVC/pom.xml

tips1 : 有關如何使用Maven的編譯設定檔pom.xml可以參考Spring官網

tips2 : 在Spring官網有提到系統記錄 logging 請使用slf4j代替舊時的commons-logging。

<?xml version="1.0" encoding="UTF-8"?>

<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>com.mytest</groupId>

     <artifactId>mvc</artifactId>

     <name>MyMVC</name>

     <packaging>war</packaging>

     <version>1.0.0-BUILD-SNAPSHOT</version>

     <properties>

         <java-version>1.8</java-version>

         <org.springframework-version>3.2.4.RELEASE</org.springframework-version>

         <org.aspectj-version>1.8.5</org.aspectj-version>

         <org.slf4j-version>1.7.12</org.slf4j-version>

     </properties>

     <dependencies>

         <!-- Spring -->

         <dependency>

              <groupId>org.springframework</groupId>

              <artifactId>spring-context</artifactId>

              <version>3.2.4.RELEASE</version>

              <exclusions>

                  <!-- Exclude Commons Logging in favor of SLF4j -->

                  <exclusion>

                       <groupId>commons-logging</groupId>

                       <artifactId>commons-logging</artifactId>

                   </exclusion>

              </exclusions>

         </dependency>

         <dependency>

              <groupId>org.springframework</groupId>

              <artifactId>spring-webmvc</artifactId>

              <version>3.2.4.RELEASE</version>

         </dependency>                 

         <!-- AspectJ -->

         <dependency>

              <groupId>org.aspectj</groupId>

              <artifactId>aspectjrt</artifactId>

              <version>${org.aspectj-version}</version>

         </dependency>

 

         <!-- Logging -->

         <dependency>

              <groupId>org.slf4j</groupId>

              <artifactId>slf4j-api</artifactId>

              <version>${org.slf4j-version}</version>

         </dependency>

         <dependency>

              <groupId>org.slf4j</groupId>

              <artifactId>jcl-over-slf4j</artifactId>

              <version>${org.slf4j-version}</version>

              <scope>runtime</scope>

         </dependency>

         <dependency>

              <groupId>org.slf4j</groupId>

              <artifactId>slf4j-log4j12</artifactId>

              <version>${org.slf4j-version}</version>

              <scope>runtime</scope>

         </dependency>

         <dependency>

              <groupId>log4j</groupId>

              <artifactId>log4j</artifactId>

              <version>1.2.15</version>           

              <scope>runtime</scope>

         </dependency>

 

         <!-- @Inject -->

         <dependency>

              <groupId>javax.inject</groupId>

              <artifactId>javax.inject</artifactId>

              <version>1</version>

         </dependency>

                 

         <!-- Servlet -->

         <dependency>

              <groupId>javax.servlet</groupId>

              <artifactId>servlet-api</artifactId>

              <version>2.5</version>

              <scope>provided</scope>

         </dependency>

         <dependency>

              <groupId>javax.servlet.jsp</groupId>

              <artifactId>jsp-api</artifactId>

              <version>2.1</version>

              <scope>provided</scope>

         </dependency>

         <dependency>

              <groupId>javax.servlet</groupId>

              <artifactId>jstl</artifactId>

              <version>1.2</version>

         </dependency>

     </dependencies>

    <build>

        <plugins>

            <plugin>

                <artifactId>maven-eclipse-plugin</artifactId>

                <version>2.9</version>

                <configuration>

                    <additionalProjectnatures>

                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>

                    </additionalProjectnatures>

                    <additionalBuildcommands>

                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>

                    </additionalBuildcommands>

                    <downloadSources>true</downloadSources>

                    <downloadJavadocs>true</downloadJavadocs>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>2.5.1</version>

                <configuration>

                    <source>1.6</source>

                    <target>1.6</target>

                    <compilerArgument>-Xlint:all</compilerArgument>

                    <showWarnings>true</showWarnings>

                    <showDeprecation>true</showDeprecation>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.codehaus.mojo</groupId>

                <artifactId>exec-maven-plugin</artifactId>

                <version>1.2.1</version>

                <configuration>

                    <mainClass>org.test.int1.Main</mainClass>

                </configuration>

            </plugin>

        </plugins>

    </build>

</project>

(9)使用maven編譯MyMVC project。點選 MyMVC > Maven > Update Project

執行時會根據pom.xml所設定dependencygroupIdartifactIdversion到你本機的

這個路徑 C:\Users\Karen\.m2\repository 尋找相應的jar檔,找不到時,就會從

Maven Repository下載到本機。

Image 1  

五. 新增Tomcat Server

(1) File > New > Other

Image 2  

(2)選擇Tomcat 8.0

Image 3  

 (3)選擇專案MyMVC

Image 4  

(4)開啟server.xml,新增一小段設定內容

Image 5

<Engine>

     …

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"> 

        

        <!-- path為程式啟始路徑/mvc,目前根目錄路徑為http://localhost:8080/mvc -->

        <Context docBase="MyMVC" path="/mvc" reloadable="true"            source="org.eclipse.jst.jee.server:MyMVC"/>

</Host>

</Engine>

 六. 啟動Tomcat及執行結果

(1)選擇 Window > Show View > Servers

啟動Tomcat

Image 7  

(2)http://localhost:8080/mvc/hello

Image 8  

(3)http://localhost:8080/mvc/

Image 6  

 

 

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 karenjian822 的頭像
    karenjian822

    Karen's note

    karenjian822 發表在 痞客邦 留言(0) 人氣()