文章目录
1.模块创建 1.创建一个webapp的maven项目 2.目录结构
2.代码 1.HomeController.java 2.home.jsp 3.applicationContext.xml Spring配置文件 4.spring-mvc.xml SpringMVC配置文件 5.web.xml 配置中央控制器以及Spring和SpringMVC配置文件的路径 6.index.jsp
3.配置Tomcat 1.配置路径以及热加载 2.配置war包以及上下文路径为/ 3.启动
4.初始化基本流程解析
1.模块创建
1.创建一个webapp的maven项目
2.目录结构
2.代码
1.HomeController.java
package com. example. controller ;
import org. springframework. stereotype. Controller ;
import org. springframework. ui. Model ;
import org. springframework. web. bind. annotation. RequestMapping ;
@Controller
public class HomeController {
@RequestMapping ( "/home" )
public String home ( Model model) {
System . out. println ( "SpringMVC 执行了 home() 方法" ) ;
model. addAttribute ( "message" , "Hello, SpringMVC!" ) ;
return "home" ;
}
}
2.home.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> SpringMVC Demo < / title>
< / head>
< body>
< h2> ${ message} < / h2>
< / body>
< / html>
3.applicationContext.xml Spring配置文件
< 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"
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" >
< context: component-scan base-package = " com.example.service" />
</ beans>
4.spring-mvc.xml SpringMVC配置文件
< 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 http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
< mvc: annotation-driven/>
< context: component-scan base-package = " com.example.controller" />
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /WEB-INF/views/" />
< property name = " suffix" value = " .jsp" />
</ bean>
</ beans>
5.web.xml 配置中央控制器以及Spring和SpringMVC配置文件的路径
<?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns = " http://xmlns.jcp.org/xml/ns/javaee"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version = " 4.0" >
< servlet>
< servlet-name> dispatcher</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< init-param>
< param-name> contextConfigLocation</ param-name>
< param-value> /WEB-INF/spring-mvc.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> dispatcher</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener</ listener-class>
</ listener>
< context-param>
< param-name> contextConfigLocation</ param-name>
< param-value> /WEB-INF/applicationContext.xml</ param-value>
</ context-param>
</ web-app>
6.index.jsp
< html>
< body>
< h2> Hello World ! < / h2>
< / body>
< / html>
3.配置Tomcat
1.配置路径以及热加载
2.配置war包以及上下文路径为/
3.启动
1.首页
2.访问 /home
4.初始化基本流程解析
Tomcat启动,读取web.xml,装载中央控制器以及获取Spring以及SpringMVC的配置文件路径 由于中央控制器配置了load-on-startup所以会调用中央控制器的init方法完成Spring以及SpringMVC容器的初始化