1_SpringMVC_概述,2_SpringMVC_项目搭建
创始人
2024-02-23 02:09:33
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

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...