springMVC-2-MVC初步了解

Spring MVC的特点

  • 轻量级,简单易学

  • 高效 , 基于请求响应的MVC框架

  • 与Spring兼容性好,无缝结合

  • 约定优于配置

  • 功能强大:RESTful、数据验证、格式化、本地化、主题等

  • 简洁灵活

Spring的web框架围绕DispatcherServlet [ 调度Servlet ] 设计。

dispatcherServlet的作用

没有dispatcherServlet前:用户传输数据,每次都要访问不同的servlet,较为麻烦

有了dispatcherServlet后:用户传输数据,只需要去访问dispatcherServlet,他会帮助我们去调度不同的servlet

自动生成dispatcherServlet快捷键:alt+insert

image-20210502141736753

搭建springMVC项目

1、新建一个Moudle , springmvc-02-hello , 添加web的支持!

image-20210503161543498

2、确定导入了SpringMVC 的依赖!

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>5.0.2.RELEASE</spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

3、配置web.xml , 注册DispatcherServlet

快捷键:alt+insert

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


    <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-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

4、创建控制器

package com.wang.web;

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

@Controller
@RequestMapping("mycontrol")
public class MyControl {
    @RequestMapping("test1")
    public String test1(){
        System.out.println("跳转成功");
        return "hello";
    }
}

5、创建spring配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--注解扫描-->
    <context:component-scan base-package="com.wang.web"></context:component-scan>

    <!--注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--视图解析器-->
    <!--跳转到工程目录下/WEB-INF/pages/返回字符串.jsp-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

6、创建需要的jsp界面

/WEB-INF/pages/hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>
这是跳转界面
</body>
</html>

7、添加lib依赖(IDEA需要)

image-20210503162020720
posted @ 2021-06-07 16:09  Coder-Wang  阅读(43)  评论(0编辑  收藏  举报