M model 模型层 DAO封装 >>> Mybatis
V view 视图层 html css js jsp
C controller 控制层 Servlet封装 >>> springMVC
SpringMVC是spring为展现层提供的基于MVC设计理念的优秀WEB框架,是目前最主流的MVC框架之一
SpringMVC通过一套注解,可以让普通的JAVA类成为contrllor控制器,无需继承Servlet,实现了控制层和Servlet之间的解耦
SpringMVC支持Rest风格的URL写法
SpringMVC采用了松耦合,可热插的主键结构,比其他的框架更具扩展性和灵活性
2_SpringMVC_项目搭建
1创建空项目 项目和maven web模块
设置maven和 lombok
创建maven web module
注意选择骨架为maven-archetype-webapp
键入GroupID和 artfactid
补充项目结构文件夹并标记文件夹
创建好目录后,选中目录,右击 mark directory as 选择对应目录类型即可
修改web.xml 中的版本约束
可以创建一个javaEE项目,然后复制web.xml文件中的内容即可
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,然后跳转至JSP
导入依赖
创建servlet
package com.msb.controller;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
@WebServlet("/myServlet.do")
public class MyServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("first.jsp").forward(req,resp);
}
}
准备一个first.jsp(略)
配置Tomcat启动运行项目
添加运行的外部Tomcat环境
将当前模块放入Tomcat
启动测试: 略
2导入jar依赖
3在web.xml中配置DispatcherServlet
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
4加入SpringMVC的配置文件
在resources下添加 springmvc.xml
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
添加log4j2.xml
5编写controller层处理器
package com.msb.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
@Controller
@RequestMapping("/msb")
public class FirstController {
@RequestMapping("/firstController.do")
public String firstController(){
System.out.println("this is firstController");
return "/first.jsp";
}
}
6编写视图层
<%--
Created by IntelliJ IDEA.
User: Mark70
Date: 2021/4/12
Time: 12:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>