一、使用IDEA建立SpringMVC程式步驟圖解
第1步:建立Maven工程,選擇webapp

第2步:起名字,選項目儲存位置
第3步:配置Maven等,一般不用改動
第4步:配置項目運作環境(Tomcat)
第5步:選擇Tomcat——Local
第6步:配置Tomcat
第7步:配置所部署的項目
第8步:選擇要部署的項目(選第一個)
第9步:Application Context設定說明
第10步:建立java和resources檔案夾
第11步:根據智能提示建立即可
第12步:建立包com.zibo.controller
第13步:建立控制器HelloController類
第14步:輸入名字選擇Class即可
第15步:往pom.xml添加相應的SpringMVC架構坐标,詳見代碼
第16步:修改HelloController類,詳見代碼
第17步:删除預設的index.jsp頁面
第18步:建立新的index.jsp頁面
第19步:輸入名字,并選擇jsp檔案
第20步:修改index.jsp頁面,詳見代碼
第21步:建立spring-mvc.xml配置檔案
第22步:寫入配置内容,詳見代碼
第23步:配置(建立)Application Context
第24步:起個名字,建立即可
第25步:建立一個pages檔案夾,以後将jsp頁面放入此檔案夾
第26步:建立success.jsp頁面,詳見代碼
第27步:修改web.xml配置檔案(不必在意web-app标紅),詳見代碼
第28步:啟動Tomcat,直到項目部署完成
第29步:浏覽器自動跳轉到此頁面
第30步:點選超連結,完成入門
檔案位置圖:
二、代碼
HelloController控制器類:
package com.zibo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//控制器
@Controller
public class HelloController {
//設定路徑為"/hello"
@RequestMapping(path = "/hello")
public String sayHello(){
//控制台列印"Hello Spring MVC!"
System.out.println("Hello Spring MVC!");
//傳回success.jsp頁面
return "success";
}
}
index.jsp頁面:
Created by IntelliJ IDEA.
User: ZiBo
Date: 2020/8/9
Time: 14:52
To change this template use File | Settings | File Templates.
--%>
Title
入門程式!
點我調轉到success.jsp頁面
success.jsp頁面:
Created by IntelliJ IDEA.
User: ZiBo
Date: 2020/8/9
Time: 15:05
To change this template use File | Settings | File Templates.
--%>
Title
入門成功!
spring-mvc.xml配置檔案:
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
web.xml配置檔案:
/p>
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
Archetype Created Web Application
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
pom.xml配置檔案:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.example
SpringMVC01
1.0-SNAPSHOT
war
SpringMVC01 Maven Webapp
http://www.example.com
UTF-8
1.8
1.8
5.2.8.RELEASE
org.springframework
spring-context
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
javax.servlet
javax.servlet-api
4.0.1
provided
javax.servlet.jsp
jsp-api
2.2
provided
SpringMVC01
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
三、SpringMVC執行流程圖
本文位址:https://blog.csdn.net/qq_29689343/article/details/107894868
希望與廣大網友互動??
點此進行留言吧!