欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Eclipse使用maven搭建spring mvc圖文教程

 更新時間:2016年05月25日 08:48:43   作者:wenxuechaozhe  
這篇文章主要為大家分享了Eclipse使用maven搭建spring mvc圖文教程,感興趣的小伙伴們可以參考一下

Eclipse使用maven搭建spring mvc的詳細(xì)步驟,供大家參考,具體內(nèi)容如下

1、 環(huán)境配置

a). Java 1.7

b). Eclipse luna

c). Maven3.2.5

d). Spring 4.1.4

2、 創(chuàng)建maven工程

a). 打開eclipse,file->new->project->Maven->Maven Project


b). 下一步


c). 選擇創(chuàng)建的工程為webapp,下一步


d). 填寫項目的group id和artifact id。一般情況下,group id寫域名的倒序,artifact id寫項目名稱即可。最后點完成。


e). 最初建好后,項目目錄結(jié)構(gòu)如下


f). 一般的項目目錄中,在java Resources目錄下,還有src/main/java,src/main/test/java,src/main/test/resources這三個source folder,需要手動創(chuàng)建。在下面的步驟中會講到如何補(bǔ)齊這三個目錄。

 3、 修改項目基本設(shè)置

a). 右鍵此項目名稱->Properties->Java Build path,點擊source標(biāo)簽。

b). 提示 hello/src/main/java (missing)和hello/src/test/java (missing)。一般的項目目錄中,在java Resources目錄下,還會有src/main/test/resources這個source folder。將missing的先刪除,再重新創(chuàng)建,缺少的直接創(chuàng)建。點右鍵操作按鍵進(jìn)行刪除和添加。


c). 修改完整,效果如下圖


d). 接下來再修改libraries的配置,jre使用1.7版本。選中JRE System Library->edit ,更換版本。


e). 再修改一下order and export里的配置,主要是調(diào)整這四個目錄的顯示順序,調(diào)為自己喜歡的順序即可


f). 接下來再修改project facets,先將java修改為1.7。


Dynamic Web Module無法在這里直接修改為3.0,需要打開工程目錄下有一個.settings文件夾,打開org.eclipse.wst.common.project.facet.core.xml,做如下修改:

<installed facet="jst.web" version="3.0"/> 

重啟eclipe就可以看到更改生效了。

4、 Eclipse中maven的配置

a).  window->properties->maven,勾選 download repository index updates on startup


5、 簡單Spring mvc的配置

a). 打開項目中的pom.xml文件,并點擊Dependencies標(biāo)簽,點擊add添加新的依賴

b). 如果知道依賴的group id和artifact id,可以直接填寫,如果不清楚,可以輸入關(guān)鍵字進(jìn)行查詢,或是到http://search.maven.org網(wǎng)站查詢


c). 需要添加的依賴有:spring-webmvc,版本為4.1.4. RELEASE。完整的POM.XML文件內(nèi)容如下:

<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.springstudy</groupId> 
 
 <artifactId>study</artifactId> 
 
 <packaging>war</packaging> 
 
 <version>0.0.1-SNAPSHOT</version> 
 
 <name>study Maven Webapp</name> 
 
 <url>http://maven.apache.org</url> 
 
 <properties> 
 
     <spring.version>4.1.4.RELEASE</spring.version> 
 
   </properties> 
 
 <dependencies> 
 
 <dependency> 
 
  <groupId>junit</groupId> 
 
  <artifactId>junit</artifactId> 
 
  <version>3.8.1</version> 
 
  <scope>test</scope> 
 
 </dependency> 
 
 <dependency> 
 
 <groupId>org.springframework</groupId> 
 
 <artifactId>spring-webmvc</artifactId> 
 
 <version>${spring.version}</version> 
 
 </dependency> 
 
 </dependencies> 
 
 <build> 
 
 <finalName>study</finalName> 
 
 </build> 
 
</project> 

d).  打開src/main/webapp/WEB-INF/web.xml文件,最終修改如如下內(nèi)容:

<?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" 
 
   id="study" version="2.5"> 
 
   <display-name>Archetype Created Web Application</display-name> 
 
   <description>sprintMVC環(huán)境搭建</description> 
 
   <!-- 加載Spring配置文件 --> 
 
   <context-param> 
 
     <param-name>contextConfigLocation</param-name> 
 
     <param-value>classpath:/configs/spring-*.xml</param-value> 
 
   </context-param> 
 
   <!-- Spring監(jiān)聽 --> 
 
   <listener> 
 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 
   </listener> 
 
   <!-- Spring MVC配置 --> 
 
   <servlet> 
 
     <servlet-name>Dispatcher</servlet-name> 
 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
 
     <!-- 自定義spring mvc的配置文件名稱和路徑 --> 
 
     <init-param> 
 
       <param-name>contextConfigLocation</param-name> 
 
       <param-value>classpath:configs/spring-servlet.xml</param-value> 
 
     </init-param> 
 
     <load-on-startup>1</load-on-startup> 
 
   </servlet> 
 
   <!-- spring mvc 請求后綴 --> 
 
   <servlet-mapping> 
 
     <servlet-name>Dispatcher</servlet-name> 
 
     <url-pattern>/</url-pattern> 
 
   </servlet-mapping> 
 
   <welcome-file-list> 
 
     <welcome-file>index.jsp</welcome-file> 
 
   </welcome-file-list> 
 
</web-app> 

e). 在Java Resources/scr/main/resources目錄下,創(chuàng)建configs文件夾,以便存放在web.xml中聲明的配置路徑

f).  在Java Resources/scr/main/resources/configs目錄下,創(chuàng)建spring-servlet.xml,內(nèi)容如下:

<?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:jee="http://www.springframework.org/schema/jee" 
 
   xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 
 
   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 
 
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
 
           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
 
             http://www.springframework.org/schema/context 
 
             http://www.springframework.org/schema/context/spring-context-4.0.xsd 
 
             http://www.springframework.org/schema/jee 
 
              http://www.springframework.org/schema/jee/spring-jee-4.1.xsd 
 
              http://www.springframework.org/schema/mvc 
 
             http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
 
             http://www.springframework.org/schema/util 
 
             http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 
 
 
 
   
 
   <context:annotation-config/> 
 
   <context:component-scan base-package="com.springstudy.controller" /> 
 
   <mvc:annotation-driven /> 
 
   
 
   <mvc:resources mapping="/styles/**" location="/styles/" /> 
 
   <mvc:resources mapping="/scripts/**" location="/scripts/" /> 
 
   <mvc:resources mapping="/images/**" location="/images/" /> 
 
 
 
   <bean 
 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
 
     <property name="prefix" value="/WEB-INF/views/" /> 
 
     <property name="suffix" value=".jsp" /> 
 
   </bean> 
 
</beans> 

g). 創(chuàng)建controller包,在spring-servlet.xml文件中,<context:component-scan base-package="com.springstudy.controller" />已經(jīng)指定了路徑

h). 在src/main/webapp/WEB-INF目錄下,創(chuàng)建views文件,在spring-servlet.xml文件中,<property name="prefix" value="/WEB-INF/views/" />已指定了視圖文件路徑

i). 創(chuàng)建第一個controller文件HelloController.java,完整的文件內(nèi)容如下:

package com.springstudy.controller;  
 
import org.springframework.stereotype.Controller; 
 
import org.springframework.web.bind.annotation.RequestMapping; 
 
import org.springframework.web.servlet.ModelAndView;  
 
@Controller 
 
public class HelloController { 
 
 
 
   @RequestMapping("/hello") 
 
   public ModelAndView hello(){ 
 
     ModelAndView mv =new ModelAndView(); 
 
     mv.addObject("spring", "spring mvc"); 
 
     mv.setViewName("hello"); 
 
     return mv; 
 
   } 
 
} 

 j). 添加src/main/webapp/WEB-INF/views/hello.jsp文件,內(nèi)容如下:

<!DOCTYPE html> 
 
<html> 
 
   <head> 
 
     <meta charset="utf-8"> 
 
     <title>sprint hello</title> 
 
   </head> 
 
   <body>hello ${spring}! 
 
   </body> 
 
</html> 

6、將項目發(fā)布到tomcat

a). 在eclipse中添加tomcat 7;

b). 在tomcat添加完成后,雙擊,設(shè)置overview選項卡中Server Locations的設(shè)置;

 i.  將 Use Tomcat installation(takes control of Tomcat installation)選中

 ii.  將Deploy path的內(nèi)容改為:webapps

 iii. 保存

c). 右鍵tomcat,Add and Remove… ,添加study


d). 啟動tomcat;

e). 瀏覽器打開http://localhost:8080/study/hello,訪問成功!如下圖:

操作結(jié)束!

以上就是Eclipse使用maven搭建spring mvc的全部內(nèi)容,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • MyBatisPlus的簡介及案例詳解

    MyBatisPlus的簡介及案例詳解

    MyBatisPlus(簡稱MP)是基于MyBatis框架基礎(chǔ)上開發(fā)的增強(qiáng)型工具,旨在簡化開發(fā)、提高效率。本文將為大家詳細(xì)介紹一下MyBatisPlus是使用,需要的可以參考一下
    2022-07-07
  • SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實現(xiàn)數(shù)據(jù)庫訪問功能

    SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實現(xiàn)數(shù)據(jù)庫訪問功能

    這篇文章主要介紹了SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實現(xiàn)數(shù)據(jù)庫訪問功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • JAVA編程實現(xiàn)UDP網(wǎng)絡(luò)通訊的方法示例

    JAVA編程實現(xiàn)UDP網(wǎng)絡(luò)通訊的方法示例

    這篇文章主要介紹了JAVA編程實現(xiàn)UDP網(wǎng)絡(luò)通訊的方法,簡單說明了UDP通訊的原理并結(jié)合實例形式分析了java實現(xiàn)UDP通訊的相關(guān)類與使用技巧,需要的朋友可以參考下
    2017-08-08
  • Java中List排序的三種實現(xiàn)方法實例

    Java中List排序的三種實現(xiàn)方法實例

    其實Java針對數(shù)組和List的排序都有實現(xiàn),對數(shù)組而言你可以直接使用Arrays.sort,對于List和Vector而言,你可以使用Collections.sort方法,下面這篇文章主要給大家介紹了關(guān)于Java中List排序的三種實現(xiàn)方法,需要的朋友可以參考下
    2021-12-12
  • spring中bean的生命周期詳解

    spring中bean的生命周期詳解

    今天小編就為大家分享一篇關(guān)于spring中bean的生命周期詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • SpringBoot項目導(dǎo)入aliyun oss starter依賴后啟動報錯問題

    SpringBoot項目導(dǎo)入aliyun oss starter依賴后啟動報錯問題

    這篇文章主要介紹了SpringBoot項目導(dǎo)入aliyun oss starter依賴后啟動報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java 中IO流字符流詳解及實例

    Java 中IO流字符流詳解及實例

    這篇文章主要介紹了Java 中IO流字符流詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • springboot自定義過濾器的方法

    springboot自定義過濾器的方法

    這篇文章主要為大家詳細(xì)介紹了springboot自定義過濾器的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java SpringSecurity入門案例與基本原理詳解

    Java SpringSecurity入門案例與基本原理詳解

    這篇文章主要介紹了java中Spring Security的實例詳解的相關(guān)資料,spring security是一個多方面的安全認(rèn)證框架,提供了基于JavaEE規(guī)范的完整的安全認(rèn)證解決方案,需要的朋友可以參考下
    2021-09-09
  • springboot如何關(guān)掉tomcat容器

    springboot如何關(guān)掉tomcat容器

    這篇文章主要介紹了springboot如何關(guān)掉tomcat容器,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論