SpringMVC的工程搭建步驟實(shí)現(xiàn)
一、創(chuàng)建項(xiàng)目
1、新建一個(gè)項(xiàng)目名為:springmvc-demo-yuyongqing
右鍵項(xiàng)目名選擇Add Framework Support
2、選擇Web Application
3、配置SpringMVC
pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.13.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies>
刷新maven后再加入如下圖所示代碼
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
二、配置核心文件
1、
<?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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- bean definitions here -->
2、添加SpringMVC配置內(nèi)容
<!-- 自動(dòng)掃描包,讓指定包下的注解生效,由IOC容器統(tǒng)一管理 --> <context:component-scan base-package="controller"/> <!-- 1加載注解驅(qū)動(dòng) --> <mvc:annotation-driven/> <!-- 2靜態(tài)資源過(guò)濾 --> <mvc:default-servlet-handler/> <!-- 3視圖解析器 --> <bean id="internalResourceViewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
3、Controller層
新建一個(gè)HelloController類
package controller; @Controller public class HelloController { @RequestMapping("/hello") public String hello(Model model){ // Model 封裝數(shù)據(jù) model.addAttribute("msg","HELLO MY FIRST SPRING MVC PROJECT"); // 返回的字符串就是視圖的名字 會(huì)被視圖解析器處理 return "hello"; } }
4、JSP
在JSP包下新建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
三、web.xml
1、配置前端控制器
<!-- 配置前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
2、配置初始化參數(shù)
<!-- 配置初始化參數(shù) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </init-param>
3、設(shè)置啟動(dòng)級(jí)別
<!-- 設(shè)置啟動(dòng)級(jí)別 --> <load-on-startup>1</load-on-startup> </servlet>
4、設(shè)置SpringMVC攔截請(qǐng)求
<!-- 設(shè)置SpringMVC攔截請(qǐng)求 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern> / </url-pattern> <!--攔截除.jsp的請(qǐng)求--> </servlet-mapping>
5、亂碼過(guò)濾
<!-- 亂碼過(guò)濾 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
6、運(yùn)行web
打包
File→Project Structure
刪除默認(rèn)的包
點(diǎn)ok→ok
四、配置TomCat
1、點(diǎn)擊 Add Configuration… 進(jìn)入運(yùn)行配置框
2、點(diǎn) + 選擇Tomcat Server 下的 Local
3、點(diǎn)擊 Configure 選擇我們自己的TomCat
五、運(yùn)行TomCat
在瀏覽器輸入http://localhost:8080/hello
外鏈:
https://mvnrepository.com/
到此這篇關(guān)于SpringMVC的工程搭建步驟實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringMVC的工程搭建 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用IDEA創(chuàng)建SpringBoot項(xiàng)目的方法步驟
這篇文章主要介紹了使用IDEA創(chuàng)建SpringBoot項(xiàng)目的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Spring框架中@AliasFor注解詳細(xì)說(shuō)明
這篇文章主要給大家介紹了關(guān)于Spring框架中@AliasFor注解詳細(xì)說(shuō)明的相關(guān)資料,@AliasFor是Spring Framework中的一個(gè)注解,它用于指定注解屬性之間的別名關(guān)系,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02Java中system.exit(0) 和 system.exit(1)區(qū)別
本文主要介紹了Java中system.exit(0) 和 system.exit(1)區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Java基于elasticsearch實(shí)現(xiàn)集群管理
這篇文章主要介紹了java基于elasticsearch實(shí)現(xiàn)集群管理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02spring+srpingmvc+hibernate實(shí)現(xiàn)動(dòng)態(tài)ztree生成樹狀圖效果
這篇文章主要介紹了spring+srpingmvc+hibernate動(dòng)態(tài)ztree生成樹狀圖效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()
這篇文章主要介紹了JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()的相關(guān)資料,需要的朋友可以參考下2016-05-05理解Java當(dāng)中的回調(diào)機(jī)制(翻譯)
今天我要和大家分享一些東西,舉例來(lái)說(shuō)這個(gè)在JavaScript中用的很多。我要講講回調(diào)(callbacks)。你知道什么時(shí)候用,怎么用這個(gè)嗎?你真的理解了它在java環(huán)境中的用法了嗎?當(dāng)我也問(wèn)我自己這些問(wèn)題,這也是我開始研究這些的原因2014-10-10