SpringMVC简单执行过程

首先我们需要注意的是。如果我们的web.xml版本是2.3以下版本,EL表达式是不会自动解析的,所以,我们需要将web.xml头部更换为下图:

<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">

然后我们将要准备我们的运行;

添加jar包:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.3.9</version>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

然后我们需要配置SpringMVC的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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启扫描-->
        <context:component-scan base-package="com.openlab.controller"></context:component-scan>
<!--将注解中的返回的字符串进行拼接-->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

配置web.xml:

<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">


  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>springMVC</servlet-name>
<!--以下为SpringMVC的核心类-->
    <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>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

配置进行跳转的两个类:

package com.openlab.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.xml.ws.RequestWrapper;

@Controller
@RequestMapping("/All")
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("----------");
        return "hello";
    }
    @RequestMapping("/test")
    public String test(){
        System.out.println("----------");
        return "test";
    }
}
package com.openlab.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/dept")
public class Dept {
    @RequestMapping("/deptlist")
    public String Dept(){
        return "deptlist";
    }
}

对其进行xml文件的配置:

<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2021/9/18
  Time: 14:28
  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>
<%=new Date()%><br>
hello页面<br>

<a href="test">点击跳转到test页面</a>
<%=request.getContextPath()%>
${pageContext.request.contextPath}
<a href="${pageContext.request.contextPath}/dept/deptlist">点击跳转到dept页面</a>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2021/9/20
  Time: 17:07
  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>
    部门类
<a href="${pageContext.request.contextPath}All/hello">点击查看hello</a>

</body>
</html>

配置完成,我们使用tomcate服务器运行时,发现可以互相跳转,我们就实现了对SpringMVC的简单跳转;

最后梳理下思路:

1:首先前端需要发起请求,

2:因为我们在web.xml中配置了servlet;将url变为”/”;所以,所有的请求都会被其所接受,并传入DispatcherServlet中(SpringMVC的核心);

3:由Spring开启扫描所有的类,将控制的权限交由Spring。DispatcherServlet通过扫描后得到映射,并返回给我们的DispatcherServlet;

4:再由DispatcherServlet将结果发送给我们的InternalResourceViewResolve(视图解析器);视图解析器将我们返回的东西进行拼接,然后返回给我们的DispatcherServlet;

5:最后将结果返回给我们的用户;

总体上看的感觉就是:

用户→DispatcherServlet→控制器Controller中找到映射的url→再到InternalResourceViewResolve→最后到达用户;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值