• spring mvc 基础入门


    1.启动服务器,加载一些配置文件

    DispatcherServlet 对象被创建
    springmvc.xml 被加载
    HelloController 对象被创建
    InternalResourceViewResolver 视图解析器对象被创建
    开启mvc的注解支持

    对应代码

    初始网站页面index.jsp

    <%--
      Created by IntelliJ IDEA.
      User: ASUS
      Date: 2020/4/11
      Time: 12:56
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    
        <h3>入门程序</h3>
    
        <a href="hello">12</a>
    
    </body>
    </html>
    View Code

    springmvc.xml 配置文件:开启注解支持,开启注解bean的扫描包,实例化视图解析器对象,

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           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
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 开启注解扫描的包 -->
        <context:component-scan base-package="cn.cast"/>
    
        <!--视图解析器对象-->
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <!--开启SpringMVC框架注解的支持-->
        <mvc:annotation-driven/>
    
    </beans>
    View Code

    web.xml 配置文件:配置DispatcherServlet(拦截器)及其属性

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
    
      <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--加载配置文件-->
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!--在服务器启动时就加载该配置文件-->
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <!--拦截所有请求-->
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
    </web-app>
    View Code

    HelloContoller 类:用于处理/hello的请求

    package cn.cast.controller;
    
    /**
     * 控制器类
     */
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloController {
        /**
         * 请求映射注解:path=/hello
         *      /hello就变成这个方法执行的请求路径
         * @return
         */
        @RequestMapping(path = "/hello")
        public String sayHello(){
            System.out.println("Hello SpringMVC");
            return "success";
        }
    
    }
    View Code

    success.jsp:处理请求后返回的响应页面

    <%--
      Created by IntelliJ IDEA.
      User: ASUS
      Date: 2020/4/11
      Time: 13:15
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    
        <h3>入门成功</h3>
    
    </body>
    </html>
    View Code

    2.后台处理请求

      后台收到 <a href="hello">12</a> 的请求

      --> 所有请求被 DispatcherServlet 拦截处理

      --> 拦截器调用HelloController的sayHello(已经被配置过用来相应/hello请求)方法

      --> 方法执行完后返回 “success”

      -->拦截器通过视图解析器 解析 “success” 加上prefix和suffix,令页面跳转到success.jsp

      -->返回的相应包里就是这个success.jsp

    在 SpringMVC 的各个组件中,处理器映射器、处理器适配器、视图解析器称为 SpringMVC 的三大组件。
    使 用 <mvc:annotation-driven> 自动加载 RequestMappingHandlerMapping (处理映射器) 和
     RequestMappingHandlerAdapter ( 处 理 适 配 器 ) , 可 用 在 SpringMVC.xml 配 置 文 件 中 使 用
     <mvc:annotation-driven>替代注解处理器和适配器的配置。
     
    handlermapping,处理器映射器,顾名思义是获取映射关系的一个东西,那么结合上下文其实就是url和对应处理函数的关系
    然后是handleradapter,有了映射关系还要进一步去找到调用这个处理函数才行啊,那么这个handleradapter就是去调用具体处理函数的
  • 相关阅读:
    python学习永久存储和异常处理
    python学习os文件系统模块
    python学习文件
    python学习集合
    python学习字典
    python学习递归
    python学习函数
    python学习序列
    js加入收藏
    判断dataset和datareader中是否存在某列
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12679262.html
Copyright © 2020-2023  润新知