1_SpringMVC_概述,2_SpringMVC_项目搭建
创始人
2024-02-23 00:46:15
0

 

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文件中的内容即可

         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,然后跳转至JSP
导入依赖

 
   
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
   

   
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.3.3
      provided
   

创建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依赖


     
     
        org.springframework
        spring-context
        5.3.5
     

     
     
        org.springframework
        spring-aspects
        5.3.5
     

     
     
        aopalliance
        aopalliance
        1.0
     

     
     
        com.alibaba
        druid
        1.1.10
     

     
     
        mysql
        mysql-connector-java
        8.0.22
     

     
     
        org.springframework
        spring-jdbc
        5.3.5
     

     
     
        org.springframework
        spring-tx
        5.3.5
     

     
     
        org.springframework
        spring-orm
        5.3.5
     

     
     
        commons-logging
        commons-logging
        1.2
     

     
     
        org.apache.logging.log4j
        log4j-slf4j-impl
        2.14.0
        test
     

     
     
        org.projectlombok
        lombok
        1.18.12
        provided
     

     
     
        org.springframework
        spring-test
        5.3.5
        test
     

     
     
        org.junit.jupiter
        junit-jupiter-api
        5.7.0
        test
     

     
     
        org.springframework
        spring-web
        5.3.5
     

     
        org.springframework
        spring-webmvc
        5.3.5
     

     
     
        javax.servlet
        javax.servlet-api
        4.0.1
        provided
     

     
        javax.servlet.jsp
        javax.servlet.jsp-api
        2.3.3
        provided
     

   

3在web.xml中配置DispatcherServlet


         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">
   
   
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
       
       
       
        1
   

   
   
        dispatcherServlet
        /
   


4加入SpringMVC的配置文件

在resources下添加 springmvc.xml

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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" %>


    Title


  this is first Jsp

相关内容

热门资讯

广东省突发事件应对条例 广东省第十四届 人民代表大会常务委员会 公 告 (第70号) 《广东省突发事件应对条例》已由广东省第...
24万彩礼当场返还!安康瀛湖法... 本平台法律服务由 陕西邦彦律师事务所 提供 12月17日,汉滨法院瀛湖法庭内暖意融融,一起僵持多日的...
辅警工作近6年因有文身被辞退,... 红星新闻消息,2019年9月起,男子刘某入职吉林省农安县公安局交通管理大队,任警务辅助人员,一直到2...
北京朝阳区发布智能机器人“政策... 央广网北京12月20日消息(记者 王进文)12月19日,首届朝阳智能机器人生态大会暨Robo Sum...
《山西省供热管理条例(草案)》... 《山西省供热管理条例(草案)》 公开征求意见建议 公告 山西省第十四届人民代表大会常务委员会第二十五...
日本政策重大转向!专家警示:危... 日本央行19日宣布加息25个基点,政策利率从0.5%上调至0.75%,达30年来最高水平。 这一决策...
最高法院:既有虚假宣传,又有商... 最高法院:虚假宣传与商业诋毁构成竞合,如何处理? 经营者作虚假或者误导性宣传,同时损害竞争对手商誉的...
以色列与哈马斯均被指“消极拖延... 【环球时报驻美国特约记者 丁亮 环球时报特约记者 陈葭】美国阿克西奥斯新闻网援引白宫消息人士的话披露...
实现政策范围内分娩个人“无自付... 12月19日,省医保局发布消息,我省通过提高保障水平、优化经办服务,切实减轻参保职工生育医疗负担,2...
响应河南微短剧政策 大象新闻要... 你日常刷到的短剧,很可能不仅是“河南制造”,更是“大象出品”。 在短剧行业深耕两年多,大象新闻已悄然...