改造繼續之eclipse整合tomcat開發spring mvc項目配置一覽,tomcatmvc

來源:互聯網
上載者:User

改造繼續之eclipse整合tomcat開發spring mvc項目配置一覽,tomcatmvc

 

  在上一篇的環境配置中,你還只能基於maven開發一個javase的項目,如果要開發一個web項目,還得配置一下tomcat和spring mvc,整合一覽表如下。

 

一:Tomcat安裝

      在.net web開發中,微軟再一次向你展示了一站式馬賽克配置,你只需要輕輕一點按鈕,發射。。。一個帶有bootstrap架構的頁面就呈現在你的面前,在

java中就沒有這麼好的事情了,基本都是高清無碼。

 

1.

     http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.zip,目前tomcat最新版是 9.0 ,這裡我選擇的8.5版本。

 

 

 

2. 配置環境變數

   <1>  在windows平台下,一般會預設安裝在:C:\Program Files\Apache Software Foundation\Tomcat 8.5 目錄下,在你的windows服務列表中會有一個

   Apache Tomcat 8.5 Tomcat8服務項,這樣就算安裝完成了。

   <2> centos平台下,因為項目需要部署在linux上,wget url,解壓,最後執行startup.sh。

[root@localhost myapp]# cd tomcat[root@localhost tomcat]# lsbin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work[root@localhost tomcat]# cd bin[root@localhost bin]# lsbootstrap.jar       commons-daemon.jar            daemon.sh         setclasspath.sh  startup.sh            tool-wrapper.shcatalina.bat        commons-daemon-native.tar.gz  digest.bat        shutdown.bat     tomcat-juli.jar       version.batcatalina.sh         configtest.bat                digest.sh         shutdown.sh      tomcat-native.tar.gz  version.shcatalina-tasks.xml  configtest.sh                 setclasspath.bat  startup.bat      tool-wrapper.bat[root@localhost bin]# ./startup.shUsing CATALINA_BASE:   /usr/myapp/tomcatUsing CATALINA_HOME:   /usr/myapp/tomcatUsing CATALINA_TMPDIR: /usr/myapp/tomcat/tempUsing JRE_HOME:        /usr/mysoft/java/jdk1.8Using CLASSPATH:       /usr/myapp/tomcat/bin/bootstrap.jar:/usr/myapp/tomcat/bin/tomcat-juli.jarTomcat started.[root@localhost bin]# 

 

         從上面可以看到,tomcat已經啟動了,對了,湯姆貓的預設連接埠是8080,可以通過netstat -tln 驗證一下,最後檢查一下是否啟動正常。

 1 [root@localhost bin]# netstat -tln 2 Active Internet connections (only servers) 3 Proto Recv-Q Send-Q Local Address           Foreign Address         State       4 tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      5 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6 tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      7 tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      8 tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      9 tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     10 tcp6       0      0 :::8009                 :::*                    LISTEN     11 tcp6       0      0 :::111                  :::*                    LISTEN     12 tcp6       0      0 :::8080                 :::*                    LISTEN     13 tcp6       0      0 :::22                   :::*                    LISTEN     14 tcp6       0      0 ::1:631                 :::*                    LISTEN     15 tcp6       0      0 ::1:25                  :::*                    LISTEN     16 tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN  

二: eclipse 和 tomcat 的整合

        兩者整合起來,相對還是比較簡單的,一般來說做下面三件事情就基本可以搞定了。

1.  在eclipse 的 windows -> references -> server  ->  enveriment runtime 中先指定你的tomcat版本,比如中的

    apache tomcat 8.5版本,點擊‘next’後指定一下tomcat的安裝路徑,指定你的jre運行版本即可。

 

  2.  右擊web工程 -> Properties -> Java Build Path -> Libraries-> Add Library -> Server Runtime -> 添加tomcat ->

   切到order and export 勾選tomcat。

 

   當你終於建立好web工程之後,你會發現項目有‘錯誤資訊’:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path。

 

     這是因為你的web工程還沒有感知到jsp容器tomcat,這個時候你需要在 右擊Web工程,在Properties面板中的Java Build Path添加tomcat的library。

詳細步驟看一下標題即可,當一切做完之後,就能解決這個問題了。

 

3. 添加jstl模板引擎

      這個是可選項,如果你建立的 spring mvc工程啟動並執行時候如果報錯說缺少jstl模板,那麼在pom.xml引用一下即可。

1         <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->2         <dependency>3             <groupId>javax.servlet</groupId>4             <artifactId>jstl</artifactId>5             <version>1.1.2</version>6         </dependency>

 

 三:安裝spring mvc

        到目前為止,tomcat和eclipse的整合算是告一段落了,接下來要做的就是安裝配置spring mvc。大家可以在maven倉庫去拉一下即可,像

spring-core,spring-aop 這些必備依賴jar包會全部給你安裝完畢。

 

1. web.xml的配置

    在src-> main -> webapp -> WEB-INF 下有一個web.xml檔案,這個就相當於.net 中的web.config,在asp.net mvc 中最後是通過mvchandler進行了

請求接管,這種模式在spring mvc中同樣適用,比如:接管的Servlet是DispatcherServlet,web.xml的詳細配置如下:

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 5     <display-name>Archetype Created Web Application</display-name> 6    7     <!-- url請求攔截器 --> 8     <servlet> 9         <servlet-name>spring</servlet-name>10         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>11     </servlet>12 13     <servlet-mapping>14         <servlet-name>spring</servlet-name>15         <url-pattern>/</url-pattern>16     </servlet-mapping>17 18     <!-- 字元集過濾器 -->19     <filter>20         <filter-name>encodingFilter</filter-name>21         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>22         <init-param>23             <param-name>encoding</param-name>24             <param-value>UTF-8</param-value>25         </init-param>26         <init-param>27             <param-name>forceEncoding</param-name>28             <param-value>true</param-value>29         </init-param>30     </filter>31     <filter-mapping>32         <filter-name>encodingFilter</filter-name>33         <url-pattern>/*</url-pattern>34     </filter-mapping>35 36 </web-app>

 

2. spring-servlet.xml 配置

        我們知道spring其實就是一個bean的大容器,類的配置和管理都可以丟給spring,由於這裡spring mvc採用的是‘註解模式’,所以要定義一下‘包‘

掃描的範圍。這裡檔案名稱的定義要清楚一下:<$>-servlet.xml,其中的$就是web.xm中的<servlet-name>spring</servlet-name>的名稱spring,最後把

此檔案放置在WEB-INF檔案夾下,方便tomcat容器載入的時候進行合并讀取,詳細配置如下:

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 5     xsi:schemaLocation="http://www.springframework.org/schema/beans  6        http://www.springframework.org/schema/beans/spring-beans.xsd  7        http://www.springframework.org/schema/context  8        http://www.springframework.org/schema/context/spring-context.xsd  9        http://www.springframework.org/schema/tx 10        http://www.springframework.org/schema/tx/spring-tx.xsd11        http://www.springframework.org/schema/mvc12        http://www.springframework.org/schema/mvc/spring-mvc.xsd">13 14     <!-- 配置掃描的包 -->15     <context:component-scan base-package="com.datamip.qncrm.controller"></context:component-scan>16 17     <!-- 視圖解析器 -->18     <bean19         class="org.springframework.web.servlet.view.InternalResourceViewResolver">20         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />21         <property name="prefix" value="/WEB-INF/views/"></property>22         <property name="suffix" value=".jsp"></property>23     </bean>24 25 </beans>       

 

3. 建立mvc的views檔案夾存放所有view頁面

    在spring-servlet.xml檔案配置節的‘視圖解析器’的時候,可以看到所有的jsp頁面都要放在views檔案夾下,這裡我建立一個index.jsp檔案,詳細內容如下:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>首頁</title> 8 </head> 9 <body>10     <h1> SpringMVC 配置成功啦。。。。。</h1>11 </body>12 </html>

 

4. 建立mvc的controller控制器

    在Java Resources 中的src/main/java 目錄下,我可以建立一個HomeController.java,詳細資料如下:

 1 package com.datamip.qncrm.controller; 2  3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMethod; 6  7 @Controller 8 public class HomeController { 9     10     //路由匹配,使用index.jsp進行頁面渲染11     @RequestMapping(value="/home/index",method=RequestMethod.GET)12     public String Index() {13         return "index";14     }15 }

 

好了,基本上spring mvc配置算是結束了,接下來我們在qncrm工程目錄上 Run As -> Run As Server 執行,終於成功啦,也不容易哈~~~,相比.net 中的一站

式配置,確實難度上升了不少,不過有一句話說的好,硬是把一個做技術的程式員做成了沒技術。如果大家理解asp.net mvc的urlRoutingModule 和MvcHandler,

原理都是一樣的,希望本篇對你有協助。ZIP檔案下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.