Eclipse+maven 構建第一個簡單的springmvc項目


先給出項目的目錄:

 

在eclipse下使用maven構建第一個springmvc項目步驟如下:

1.創建maven project(此處默認你已了解maven),此處需要注意以下兩點

 

 

2.創建完畢后會看到一個 pom.xml的配置文件,此時需要引入spring web mvc的相關maven依賴,具體版本請看:MVNRepository ,一般,在這里,你可以搜索相關的maven依賴,copy到pom.xml文件即可

(copy保存后就會下載相關的包了)

我的pom.xml如下:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <groupId>com.example</groupId>
 6     <artifactId>springmvc-maven</artifactId>
 7     <packaging>war</packaging>  <!-- 使用的是war包 -->
 8     <version>0.0.1-SNAPSHOT</version>
 9     <name>springmvc-maven Maven Webapp</name>
10     <url>http://maven.apache.org</url>
11     <dependencies>
12         <dependency>
13             <groupId>junit</groupId>
14             <artifactId>junit</artifactId>
15             <version>3.8.1</version>
16             <scope>test</scope>
17         </dependency>
18 
19         <dependency>
20             <groupId>javax.servlet</groupId>
21             <artifactId>javax.servlet-api</artifactId>
22             <version>4.0.1</version>
23             <scope>provided</scope>
24         </dependency>
25 
26         <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
27         <!-- 導入springmvc的相關依賴,RELEASE是穩定版 -->
28         <dependency>
29             <groupId>org.springframework</groupId>
30             <artifactId>spring-webmvc</artifactId>
31             <version>5.1.5.RELEASE</version>
32         </dependency>
33 
34     </dependencies>
35 
36     <!-- 配置maven插件 -->
37     <build>
38         <!-- java編譯插件 -->
39         <!-- eclipse默認使用的jdk是1.5的 -->
40         <plugins>
41             <plugin>
42                 <groupId>org.apache.maven.plugins</groupId>
43                 <artifactId>maven-compiler-plugin</artifactId>
44                 <version>3.2</version>
45                 <configuration>
46                     <source>1.8</source>
47                     <target>1.8</target>
48                     <encoding>UTF-8</encoding>
49                 </configuration>
50             </plugin>
51         </plugins>
52     </build>
53 
54     <!-- <build> 55  <finalName>springmvc-maven</finalName> 56  </build> -->
57 </project>

 

 

3.配置web.xml文件,此時的web.xml在WEB-INF 目錄下,在本例子中web.xml主要是servlet的基本配置。如下:

 1 <!DOCTYPE web-app PUBLIC  2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 
 5 <web-app>
 6     <display-name>Archetype Created Web Application</display-name>
 7 
 8     <context-param>
 9         <param-name>contextConfigLocation</param-name>
10         <param-value>classpath:example-servlet.xml</param-value>
11     </context-param>
12     <!-- Could not open ServletContext resource [/WEB-INF/example-servlet.xml] -->
13     <!-- 不過這個貌似不是必須的?只要下面那個就可以?雖然會waring? -->
14     <!-- servlet的配置 -->
15     <servlet>
16         <servlet-name>example</servlet-name>
17         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
18         <!-- 在web.xml里配置需要加載的spring配置文件。 如果要裝入多個配置文件,在<param-value>標記中用逗號作分隔符即可。 -->
19         <init-param>
20             <param-name>contextConfigLocation</param-name>
21             <param-value>classpath:example-servlet.xml</param-value>
22         </init-param>
23 
24         <load-on-startup>1</load-on-startup>  <!-- 配置servlet的啟動時刻 -->
25     </servlet>
26     <servlet-mapping>
27         <servlet-name>example</servlet-name>
28         <url-pattern>/</url-pattern>  <!-- 系統中的請求經過的 -->
29     </servlet-mapping>
30 
31 
32 </web-app>

解析一下上面代碼:

第16行,建立一個名為example的servlet,根據官方文檔,你要建立一個與之對應的example-servlet.xml(記住這點)

第24行,好像是決定servlet的啟動時刻,是隨着服務器啟動還是等請求到來才啟動,上面的設置是隨着服務器啟動而啟動(這點不是很確定)

第8-11行和19-22行結合使用,因為此時使用的是maven來管理項目,它有個專門存放資源文件的目錄 src/main/resources,上面數說的建立的example-servlet.xml不是很往常一樣,直接放在WEB-INF目錄下,而是放在src/main/resources下,這兩部分是為了避免出現<!--  Could not open ServletContext resource [/WEB-INF/example-servlet.xml] --> 這個錯誤(建議讀一下官方文檔

 

 

4.接着就是在src/main/resources目錄下建立example-servlet.xml配置文件,並進行以下的配置

 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"
 4  xmlns:p="http://www.springframework.org/schema/p"
 5  xmlns:context="http://www.springframework.org/schema/context"
 6  xsi:schemaLocation="  7  http://www.springframework.org/schema/beans  8  https://www.springframework.org/schema/beans/spring-beans.xsd  9  http://www.springframework.org/schema/context 10  https://www.springframework.org/schema/context/spring-context.xsd">
11 
12     <!-- 配置controller層路徑掃描與視圖解析器 -->
13     <!--掃描controller所在的包 -->
14     <context:component-scan 15         base-package="com.example.springweb.mvc" />
16 
17     <!-- 視圖解析器 -->
18     <bean 19         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20         <property name="prefix" value="/WEB-INF/jsp/" />  <!--前綴-->
21         <property name="suffix" value=".jsp" />     <!-- 后綴 -->
22     </bean>
23 
24 
25     <!-- ... -->
26 </beans>

上面的xml中,12行-22行是新增的配置,剩余部分都是基本的,其實這部分你可以在官網找到的

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven/>

</beans>

 

在example-servlet.xml中有這樣一句配置

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

因此,要在src/main/java下新建一個 com.example.springweb.mvc 包,並在其下建立controller類

 

5.建立controller類

在src/main/java下新建一個名為 com.example.springweb.mvc 的包,並新建一個名為IndexController的java類。

IndexController.java

package com.example.springweb.mvc; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping("/home")   //這里路由映射為/home,所以http://localhost:8080/springmvc-maven/不能訪問到
    public String home() {    //這 里方法是Sring類型,因此要在WEB-INF創建一個home.jsp的頁面
        return "home"; } }

 

此時在IndexController.java定義了一個String類型返回值的方法,對應的要在 在WEB-INF創建一個名為home.jsp的頁面(原理我也不懂)

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>你好</h1>
</body>
</html>

 

最后,右鍵項目Run on Server 即可。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM