SpringMVC笔记

MVC

 

:模型(dao service)        视图(jsp)        控制器(Servlet)

SpringMVC笔记_第1张图片

 jsp本质是Servlet

model2

  1. 用户发请求

  2. Servlet接收请求数据,并调用对应的业务逻辑方法

  3. 业务处理完毕,返回更新后的数据给servlet

  4. servlet转向到JSP,由JSP来渲染页面

  5. 响应给前端更新后的页面

Controller:控制器

  1. 取得表单数据

  2. 调用业务逻辑

  3. 转向指定的页面

Model:模型

  1. 业务逻辑

  2. 保存数据的状态

View:视图

servlet回顾


        
            junit
            junit
            4.12
        
        
            org.springframework
            spring-webmvc
            5.1.9.RELEASE
        
        
            javax.servlet
            servlet-api
            2.5
        
        
            javax.servlet.jsp
            jsp-api
            2.2
        
        
            javax.servlet
            jstl
            1.2
        
    
package com.qi.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //1.获取前端参数
        String method = req.getParameter("method");
        if (method.equals("add")){
            req.getSession().setAttribute("msg","执行了add方法");
        }
        if (method.equals("delete")){
            req.getSession().setAttribute("msg","执行了delete方法");
        }

        //2.调用业务层

        //3.视图转发或者重定向
        req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req ,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }
}
<%--
  Created by IntelliJ IDEA.
  User: V556U
  Date: 2021/10/10
  Time: 10:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


${msg}




    
        hello
        com.qi.servlet.HelloServlet
    
    
        hello
        /hello
    








第一个MVC





    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:springmvc-servlet.xml
        
        
        1
    


    
    
    
        springmvc
        /
    

package com.qi.controller;


import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class HelloController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //ModelAndView 模型和视图
        ModelAndView mv = new ModelAndView();


        //封装对象,放在ModelAndView中。Model
        mv.addObject("msg","HelloSpringMVC!");
        //封装要跳转的视图,放在ModelAndView中
        mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp
        return mv;
    }


}



    
    
    
    
        
        
        
        
    

    
    

<%--
  Created by IntelliJ IDEA.
  User: V556U
  Date: 2021/10/11
  Time: 9:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


  ${msg}


使用注解开发

Meaven项目就要添加这个配置


   
       
           src/main/java
           
               **/*.properties
               **/*.xml
           
           false
       
       
           src/main/resources
           
               **/*.properties
               **/*.xml
           
           false
       
   

你可能感兴趣的:(mvc,1024程序员节)