溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么利用springmvc與maven搭建一個web項目

發布時間:2020-11-30 16:50:20 來源:億速云 閱讀:281 作者:Leah 欄目:編程語言

本篇文章為大家展示了怎么利用springmvc與maven搭建一個web項目,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

具體內容如下

1、創建一個maven project 為spring1

2、進行項目的配置:默認的java 1.5 在properties中選擇project facts項目進行配置,反選web之后修改java環境為1.8.修改之后如下圖:

怎么利用springmvc與maven搭建一個web項目

3. 配置好的工作目錄如下:

怎么利用springmvc與maven搭建一個web項目

4 修改pom.xml文件

增加兩個jar包:servlet和spring webmvc

pom.xml如下:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

4. 默認的配置文件是spring-servlet.xml ,在/web-inf下創建配置文件,內容如下:

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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://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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- 配置掃描的包 -->
<context:component-scan base-package="spring1.*" />

<!-- 注冊HandlerMapper、HandlerAdapter兩個映射類 -->
<mvc:annotation-driven />

<!-- 訪問靜態資源 -->
<mvc:default-servlet-handler />

<!-- 視圖解析器 -->
<bean
>
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

注:在這個默認的配置過程中 程序會掃描spring1包中的所有的controller

5、 在src/main/java創建controller

package spring1;

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

@Controller
public class DemoController {

@RequestMapping("/index")
public String index(){
return "index";
}
}

6、 在webapp目錄下創建index.jsp.

<html>
<body>
<h3>Hello World!</h3>
</body>
</html>

7、 部署項目到tomcat上,部署的內容如下:

怎么利用springmvc與maven搭建一個web項目

8、啟動tomcat 在瀏覽器中輸入:http://localhost:8080/spring1/

上述內容就是怎么利用springmvc與maven搭建一個web項目,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女