一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Eclipse使用maven搭建spring mvc圖文教程

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

2020-05-05 14:24wenxuechaozhe JAVA教程

這篇文章主要為大家分享了Eclipse使用maven搭建spring mvc圖文教程,感興趣的小伙伴們可以參考一下

Eclipse使用maven搭建spring mvc的詳細步驟,供大家參考,具體內容如下

1、 環境配置

a). Java 1.7

b). Eclipse luna

c). Maven3.2.5

d). Spring 4.1.4

2、 創建maven工程

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

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

b). 下一步

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

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

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

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

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

e). 最初建好后,項目目錄結構如下

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

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

 3、 修改項目基本設置

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

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

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

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

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

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

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

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

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

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

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

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

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

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

5、 簡單Spring mvc的配置

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<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文件,最終修改如如下內容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?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環境搭建</description>
 
   <!-- 加載Spring配置文件 -->
 
   <context-param>
 
     <param-name>contextConfigLocation</param-name>
 
     <param-value>classpath:/configs/spring-*.xml</param-value>
 
   </context-param>
 
   <!-- Spring監聽 -->
 
   <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目錄下,創建configs文件夾,以便存放在web.xml中聲明的配置路徑

f).  在Java Resources/scr/main/resources/configs目錄下,創建spring-servlet.xml,內容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?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). 創建controller包,在spring-servlet.xml文件中,<context:component-scan base-package="com.springstudy.controller" />已經指定了路徑

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

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

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

i). 創建第一個controller文件HelloController.java,完整的文件內容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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文件,內容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
 
<html>
 
   <head>
 
     <meta charset="utf-8">
 
     <title>sprint hello</title>
 
   </head>
 
   <body>hello ${spring}!
 
   </body>
 
</html>

6、將項目發布到tomcat

a). 在eclipse中添加tomcat 7;

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

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

 ii.  將Deploy path的內容改為:webapps

 iii. 保存

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

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

d). 啟動tomcat;

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

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

操作結束!

以上就是Eclipse使用maven搭建spring mvc的全部內容,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产在线午夜视频无 | 国内精品91最新在线观看 | 91青青国产在线观看免费 | 久久精品在现线观看免费15 | 全程粗语对白视频videos | 无限在线观看视频大全免费高清 | 99国产精品热久久久久久夜夜嗨 | 猛男壮男受bl爽哭了高h | 天天白天天谢天天啦 | 99热在线只有精品 | 欧美粗黑巨大gay | 爽好舒服把腿张小说 | 黑人开嫩苞| 亚洲国产精品久久人人爱 | 四虎在线播放 | 波多野结衣之双方调教在线观看 | 日本黄色影院 | 嫩草在线观看视频 | 我的家教老师在线观看 | 91久久综合 | 久久久久琪琪精品色 | 特黄特色大片免费高清视频 | 男人懂得网站 | 国产福利视频一区二区微拍视频 | 果冻传媒天美传媒在线小视频播放 | 操儿子| 亚洲精品乱码久久久久久蜜桃欧美 | 国产99久久九九精品免费 | 亚洲精品无码久久不卡 | 扒开胸流出吃奶 | 草莓永久地域网名入2022 | 牛牛色婷婷在线视频播放 | 免费一级片在线 | 国产精品欧美亚洲韩国日本99 | 国内精品在线观看视频 | 日产精品一卡2卡三卡4乱码久久 | 美女又爽又黄免费 | 女人pp被扒开流水了 | 第一次做m被调教经历 | 2021麻豆剧果冻传媒入口永久 | 日韩福利网站 |