SpringMVC的工程搭建步驟實現
一、創建項目
1、新建一個項目名為:springmvc-demo-yuyongqing
右鍵項目名選擇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配置內容
<!-- 自動掃描包,讓指定包下的註解生效,由IOC容器統一管理 --> <context:component-scan base-package="controller"/> <!-- 1加載註解驅動 --> <mvc:annotation-driven/> <!-- 2靜態資源過濾 --> <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層
新建一個HelloController類
package controller; @Controller public class HelloController { @RequestMapping("/hello") public String hello(Model model){ // Model 封裝數據 model.addAttribute("msg","HELLO MY FIRST SPRING MVC PROJECT"); // 返回的字符串就是視圖的名字 會被視圖解析器處理 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、配置初始化參數
<!-- 配置初始化參數 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </init-param>
3、設置啟動級別
<!-- 設置啟動級別 --> <load-on-startup>1</load-on-startup> </servlet>
4、設置SpringMVC攔截請求
<!-- 設置SpringMVC攔截請求 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern> / </url-pattern> <!--攔截除.jsp的請求--> </servlet-mapping>
5、亂碼過濾
<!-- 亂碼過濾 --> <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、運行web
打包
File→Project Structure
刪除默認的包
點ok→ok
四、配置TomCat
1、點擊 Add Configuration… 進入運行配置框
2、點 + 選擇Tomcat Server 下的 Local
3、點擊 Configure 選擇我們自己的TomCat
五、運行TomCat
在瀏覽器輸入http://localhost:8080/hello
外鏈:
https://mvnrepository.com/
到此這篇關於SpringMVC的工程搭建步驟實現的文章就介紹到這瞭,更多相關SpringMVC的工程搭建 內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- None Found