SpringMVC框架搭建流程(完整详细版)
                SpringMVC框架搭建流程
开发过程
1)配置DispatcherServlet前端控制器
2)开发处理具体业务逻辑的Handler(@Controller、 @RequestMapping)
3) xml配置⽂件配置controller扫描,配置springmvc三⼤件
4)将xml⽂件路径告诉springmvc(DispatcherServlet)
详细流程:
创建目录
新建maven项目,注意选择webapp骨架。
创建成功之后会发现没有src等目录,这些需要我们手动创建:
在src下面新建main,main下面新建java目录,选择java目录,右键,
在main下面继续新建resource目录,选择resource目录,右键,
pom.xml
pom.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
注意Tomcat7插件是用来运行项目的,右侧运行:
springmvc相关配置
main文件夹下面新建webapp文件夹,webapp下面新建WEB-INF,下面新建web.xml
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
里面配置了springmvc相关的配置,引入了springmvc.xml:
在resource目录下新建springmvc.xml:
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 https://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 ">
在java目录下新建包com.lagou.edu.controller,下面新建DemoController:
package com.lagou.edu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
/**
* @author lyj
* @Title: DemoController
* @ProjectName springmvc-demo
* @Description: TODO
* @date 2020/6/9 21:21
*/
@Controller
@RequestMapping("/demo")
public class DemoController {
/**
* http://localhost:8080/demo/handle01
*/
@RequestMapping("/handle01")
public ModelAndView handle01(){
Date date=new Date();
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("date",date);
modelAndView.setViewName("success");
return modelAndView;
}
}
在WEB-INF下面新建jsp文件夹,下面新建success.jsp:
<%@ page language="java" isELIgnored="false" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
当前时间 ${date}
完毕后整个项目结构如下:
测试:
浏览器访问: