SpringMVC_2023_11_27_1 SpringMVC_入门

发布时间 2023-11-28 16:32:01作者: Kbaor

SpringMVC_入门

2023-11-28 16:11:38 星期二

Spring MVC 是 Spring 提供给 Web 应用的框架设计。。
Spring MVC 角色划分清晰,分工明细,并且和 Spring 框架无缝结合。作为当今业界最主流的 Web 开发框架,Spring MVC 已经成为当前javaWeb框架事实上的标准。

SpringMVC核心组件

a) 前端控制器(DispatcherServlet) 配置

i. 接收请求,响应结果,相当于电脑的CPU

b) 处理器映射器(HandlerMapping) 配置

i. 根据url查找对应的处理器

c) 处理器(Handler) 程序员写

i. 处理核心的业务逻辑

d) 处理器适配器(HandlerAdapter) 配置

i. 会把处理器包装成适配器,这样可以支持多种类型的处理器

e) 视图解析器(ViewResovler) 配置

i. 进行视图解析,并返回字符串,进行处理,可以解析成对应的页面

Springmvc执行流程(围绕以上组件)

image

Springmvc项目的创建--(XML)

1. 依赖的引入

 </dependency>
       <!-- spring基础依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.20</version>
        </dependency>
      <!--  springweb依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--springmvc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.20</version>
        </dependency>

2. Springmvc配置文件--(Spring-mvc.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
        处理映射器
        BeanNameUrlHandlerMapper
    -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--
        处理器适配器
        SimpleControllerHandlerAdapter
    -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
   <!--
        处理器
        自定义实现Controller接口(对应:SimpleControllerHandlerAdapter)
   -->
    <bean name="/hello.do" class="com.bboy.controller.StudentController"/>
    <!--
        视图解析器
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--添加跳转目录的前缀-->
        <property name="prefix" value="/WEB-INF/"/>
        <!--添加跳转目录的后缀缀-->
        <property name="suffix" value=".jsp"/>
    </bean>



</beans>

3. 配置前端控制器--(在web.xml中进行配置)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         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">
    <!--SpringMVC前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--
        在前端控制器中加载springmvc的配置文件,例如:spring-mvc.xml   
    -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--
             配置哪些请求进入到:前端控制器
        -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

4. 配置处理器映射器--(在springmvc的配置文件中进行配置)

<!--
        处理映射器
        BeanNameUrlHandlerMapper
    -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

5. 配置处理器适配器--(在springmvc的配置文件中进行配置)

<!--
        处理器适配器
        SimpleControllerHandlerAdapter
    -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

6. 配置处理器(处理业务逻辑)

i. 自定义类实现Controller接口

package com.bboy.controller;

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

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

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/27 0027 10:53:23
 */
public class StudentController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //- 创建ModelAndView
        ModelAndView mav = new ModelAndView();
        //- 添加数据
        mav.addObject("uname","tom");
        mav.addObject("age",20);
        //- 方法一:跳转到页面:show.jsp页面
        //mav.setViewName("/show.jsp");
        //System.out.println("hello==============>StudentController");
        //- 方法二:结合 视图解析器进行跳转
        mav.setViewName("show");
        System.out.println("hello==============>StudentController");
        return mav;
    }
}

ii. 在springmvc核心配置文件中进行controller的注册

<!--
        处理器
        自定义实现Controller接口(对应:SimpleControllerHandlerAdapter)
   -->
    <bean name="/hello.do" class="com.bboy.controller.StudentController"/>

7. 视图解析器--(在springmvc的配置文件中进行配置)

<!--
        视图解析器
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--添加跳转目录的前缀-->
        <property name="prefix" value="/WEB-INF/"/>
        <!--添加跳转目录的后缀缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

8. ModelAndView

a) Model 存储数据
b) View 指定视图(页面)b) View 指定视图(页面)

具体使用:

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //- 创建ModelAndView
        ModelAndView mav = new ModelAndView();
        //- 添加数据
        mav.addObject("uname","tom");
        mav.addObject("age",20);
        //- 方法一:跳转到页面:show.jsp页面
        //mav.setViewName("/show.jsp");
        //System.out.println("hello==============>StudentController");
        //- 方法二:结合 视图解析器进行跳转
        mav.setViewName("show");
        System.out.println("hello==============>StudentController");
        return mav;
    }

在页面上,可以使用EL表示将modelAndView中的数据进行显示

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2023/11/27 0027
  Time: 11:27:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
展示页面<br>
${uname}-----${age}
</body>
</html>

9. 将show.jsp页面放到WEB-INF下

a) 在WEB-INF下的页面都是被保护的,不能通过浏览器直接访问,需要通过后台的跳转进行访问
b) 结合视图解析器进行跳转
image
image