SpringMVC概念和第一个程序

根据B站视频整理:https://www.bilibili.com/video/BV1Mt411G7A8?p=1

1、SringMVC 概念

1.1、三层架构

1、我们开发架构一般都是基于两种形式:
一种是 Client/Server 架构(C/S架构),也就是客户端/服务器;
另一种是 Browser/Server 架构(B/S架构),也就是浏览器/服务器。

2、在JavaEE开发种,几乎全都是基于B/S架构的开发。在B/S架构中,又分成了三层架构。

3、三层架构
表现层:web层,用来和客户端进行数据交互的,表现层一般采用MVC设计模型。
业务层:写业务逻辑代码
持久层:用来操作数据库的

1.2、MVC模型

1、MVC是模型(model)-视图(view)-控制器(controller)的缩写,模型视图控制器。
2、Model:数据模型,JavaBean的类,用来进行数据封装。
3、View:指用JSP、HTML来站视数据给用户
4、Controller:用来接受用户的请求,整个流程的控制器。
5、角色:

前端控制器(DispatcherServlet)

处理映射器(HandlerMapping)

处理适配器(HandlerAdapter)

视图解析器(ViewResolver)

处理器或页面控制器(Controller)

验证器(Validator)

命令对象(Command)

表单对象(From Object)

1.3、第一个SpringMVC程序

步骤:

1、建立一个maven项目,勾选web框架,创建java包和resource包,右击,把它们作为source root和resource root。

2、导入pom依赖坐标,在resources目录下,新建配置文件springmvc.xml。

3、编写index.jsp,在web.xml中添加拦截器。

4、编写controller类,导入tomcat服务器。

5、运行测试

  • 整个项目结构

在这里插入图片描述

详细步骤:

1、建立一个maven项目,勾选web框架,创建java包和resource包,右击,把它们作为source root和resource root,如下图。

在这里插入图片描述

2、导入pom依赖坐标,在resources目录下,新建配置文件springmvc.xml。

pom依赖

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

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

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

springmvc.xml配置文件。因为要开启注解扫描和用mvc,所以要加上mvc和context,添加如下图。

在这里插入图片描述

完整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:mvc="http://www.springframework.org/schema/mvc"
       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/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="com.lu"></context:component-scan>

    <!--视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--开启SpringMVC框架注解的支持-->
    <mvc:annotation-driven/>

</beans>

3、编写index.jsp,在web.xml中添加拦截器。

index.jsp中在第一行添加,防止乱码;在body标签中添加a标签。

<%--防止中文乱码--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>入门程序</h3>
    <a href="hello">入门程序</a>
</body>
</html>

在web.xml中,添加拦截器和加载配置文件

  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!--加载springmvc配置文件-->
    <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>
  1. load-on-startup 元素标记容器是否应该在web应用程序启动的时候就加载这个servlet,(实例化并调用其init()方法)。
  2. 它的值必须是一个整数,表示servlet被加载的先后顺序。
  3. 如果该元素的值为负数或者没有设置,则容器会当Servlet被请求时再加载
  4. 如果值为正整数或者0时,表示容器在应用启动时就加载并初始化这个servlet,值越小,servlet的优先级越高,就越先被加载。值相同时,容器就会自己选择顺序来加载。

4、编写controller类,导入tomcat服务器。

编写类HelloController

//控制器
@Controller
public class HelloController {
    
    

    @RequestMapping(path = "/hello")
    public String syaHello(){
    
    
        System.out.println("Hello SpringMVC!");
        return "success";
    }
}

返回的是一个success界面。在WEB-INF包下边建一个pages包,在包里面建一个success.jsp,作为一个点击后的结果,在springmvc.xml中也加了视图解析器,为的是可以找到这个文件。

编写success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>入门成功</h3>
</body>
</html>

怎么导入tomcat,详细看这篇文章:https://blog.csdn.net/qq_42524288/article/details/103304138

5、运行测试

运行后界面:

在这里插入图片描述
点击入门程序后跳转到hello界面:

在这里插入图片描述

1.4、第一个SpringMVC程序执行流程

在这里插入图片描述

1.5、RequestMapping注解

RequestMapping是一个用来处理请求地址映射的注解。
可以作用在类上和方法上。作用在类上,类中的方法都是以该路径作为父类路径。

RequestMapping注解有六个属性

  • value:指定请求的url。它和path属性的作用一样。

  • method:指定请求的方式。例如GET、POST、PUT、DELETE等。

  • params:指定请求中必须包含某些参数值是,才让该方法处理。

  • headers:用于指定限制请求消息头的条件。

在index.jsp添加标签:

<a href="testRequestMapping">testRequestMapping</a>

在测试类HelloController中添加::

    //请求方式为post
    //@RequestMapping(value = "/testRequestMapping", method = {RequestMethod.POST})
    //请求参数必须包含username=lu,否则请求失败
    //@RequestMapping(value = "/testRequestMapping",params = {"username=lu"})
    //请求头必须包含Accept,否则请求失败
    @RequestMapping(value = "/testRequestMapping",headers = {
    
    "Accept"})
    public String testRequestMapping(){
    
    
        System.out.println("测试RequestMapping...");
        return "success";
    }

猜你喜欢

转载自blog.csdn.net/qq_42524288/article/details/109132711