• Spring MVC 理解程序


    Spring MVC 理解程序

    可以对照这篇博客进行理解:https://www.cnblogs.com/love2000/p/14166696.html

     1.导入依赖:

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.0.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.2</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
      </dependencies>

     2.配置DispatchServlet

    <?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">
      <!--    配置DispatchServlet 这个是springMVC的核心;请求分发器,前端控制器-->
      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
    <!--  DispatchServlet要绑定的配置文件  -->
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
    <!--    启动级别-->
        <load-on-startup>1</load-on-startup>
      </servlet>
    <!--
    在SpringMVC中,/ /*
    /:只匹配所有的请求,不会去匹配jsp页面
    /*:匹配所有的请求,包括jsp页面
    
    -->
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
    </web-app>

     3.编写DispatchServlet绑定的配置文件:

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--    处理器映射器-->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--    处理器适配器-->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!--    视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/> <!--前缀-->
            <property name="suffix" value=".jsp"/> <!--后缀-->
        </bean>
    
    </beans>

     4.编写Controller:

    package com.xiaofu.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 httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
            ModelAndView mv = new ModelAndView();
            //业务代码
            String result = "helloSpringMvc";
            mv.addObject("msg",result);
    
            //视图跳转
            mv.setViewName("text");
            return mv;
        }
    }

    5.注册刚刚写好的Controller:

     <bean id="/hello" class="com.xiaofu.controller.HelloController"/>

    6.创建要跳转的jsp:

    <%--
      Created by IntelliJ IDEA.
      User: 86176
      Date: 2021/1/12
      Time: 11:36
      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>
    
    ${msg}
    </body>
    </html>

    7.配置tomcat:

    博客:https://www.cnblogs.com/love2000/p/14164947.html

    8.测试:

     启动tomcat

     输入地址访问成功!

     

  • 相关阅读:
    React开发实时聊天招聘工具 -第六章 登陆注册(2)
    React开发实时聊天招聘工具 -第六章 登陆注册(1)
    温习 socket http tcp
    高阶组件简介
    计算机组成原理(7)——输入、输出系统
    计算机组成原理(6)——总线
    计算机组成原理(5)——中央处理器
    计算机组成原理(4)——指令系统
    计算机组成原理(3)——存储层次结构
    计算机组成原理(2)——数据的表示与运算
  • 原文地址:https://www.cnblogs.com/love2000/p/14266246.html
Copyright © 2020-2023  润新知