一、使用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
希望与广大网友互动??
点此进行留言吧!