springMVC详细介绍

时间:2022-09-23 08:51:28

springMVC简介

springMVC又 叫Spring web mvc。是Spring框架中的一部分,是Spring3.0后发布的。

SpringMVC框架的优点

1、基于MVC架构模式:MVC架构模式

2、容易理解上手快,使用简单:可以开发一个注解的SpringMVC项目,SpringMVC也是轻量级的框架,jar包很小,不依赖特定的接口和类。

3、作为Spring的一部分,能够使用Spring框架的IOC容器和Aop编程。方便整合Strtus、MyBatis、Hibernate、JPA等其他框架。

4、SpringMVC强化注解的使用,在控制器,Service、Dao都可以使用注解,方便灵活。使用@Controller创建对象容器,@Service创建业务对象,@AutoWired或者@Resource在控制器类中注入Service,Service类中注入Dao。

第一个SpringMVC程序

所谓SpringMVC注解式开发是指,在代码中通过对类与方法的注解,便可以完成处理器在springMVC容器的注册。注册是开发的重点

步骤

第一步:创建maven-web项目

springMVC详细介绍

第二步:在pom.xml中添加依赖和插件

加入servlet、jsp、springmvc依赖

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <!--加入servlet依赖-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version> <scope>provided</scope>
    </dependency>
    <!-- jsp依赖 -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2.1-b03</version>
        <scope>provided</scope>
    </dependency>
    <!--加入springMVC依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.5.RELEASE</version>
    </dependency>
</dependencies>

加入插件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<resources>
    <resource>
        <directory>src/main/java</directory><!--所在的目录-->
        <includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>
<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>

第三步注册中央调度器

中央调度器为一个Servlet,名称为DispatcherServlet中央调度器的全限定性类名在导入的 Jar 文件 spring-webmvc-5.2.5.RELEASE.jar 的第一个包 org.springframework.web.servlet下可找到

<load-on-startup/>标签:

在<servlet/>中添加<load-on-startup/>的作用是,标记是否在Web服务器(这里是Tomcat)启动时会创建这个 Servlet 实例,即是否在 Web 服务器启动时调用执行该 Servlet 的 init()方法,而不是在真正访问时才创建。它的值必须是一个整数。

当值大于等于 0 时,表示容器在启动时就加载并初始化这个 servlet,数值越小,该 Servlet的优先级就越高,其被创建的也就越早;

当值小于 0 或者没有指定时,则表示该 Servlet 在真正被使用时才会去创建。

当值相同时,容器会自己选择创建顺序。

<url-pattern/>标签:

可以写为 / ,建议写为*.do 的形式

配置文件的位置和名称

注册完毕后,可直接在服务器上发布运行。此时,访问浏览器页面,控制台均会抛出FileNotFoundException 异常。即默认要从项目根下的 WEB-INF 目录下找名称为 Servlet 名称-servlet.xml 的配置文件。这里的“Servlet 名称”指的是注册中央调度器<servlet-name/>标签中指定的 Servlet 的 name 值。本例配置文件名为 springmvc-servlet.xml。

而一般情况下,配置文件是放在类路径下的,即resources目录下,所以在注册中央调度器,需要为sprigMVC配置文件路径

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--配置springMVC核心容器对象DispatcherServlet-->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationcontext-servlet.xml</param-value>
    </init-param>
    <!--在Tomcat启动后,创建servlet对象
    load-on-startup:表示Tomcat启动后创建对象的顺序,它的值是整数,数值越小,Tomcat创建对象的事件越早,大于等于0
    -->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--可以写为 / ,建议写为*.do 的形式-->
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

第四步:创建springMVC配置文件

声明组件扫描器

配置视图解析器

SpringMVC 框架为了避免对于请求资源路径与扩展名上的冗余,在视图解析器InternalResouceViewResolver 中引入了请求的前辍与后辍。而 ModelAndView 中只需给出要跳转页面的文件名即可,对于具体的文件路径与文件扩展名,视图解析器会自动完成拼接。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启注解扫描-->
    <context:component-scan base-package="school.xauat"></context:component-scan>
    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

第五步:创建Controller类

使用@Controller注解

使用@RequestMapping注解

@RequestMapping:(可以加载在类上面,也可以加载在方法上,若类和方法上都加的有,先访问类,再访问方法)

  • 设置请求映射,把请求和控制层中的方法设置映射关系
  • 当请求路径和@RequestMapping的value属性值一样时,则该注解所标注的方法即为处理请求的方法

method:用来设置请求方式,只有客户端发送请求的方式和method的值一致,才能处理请求

springMVC详细介绍

请求方式:GET 查询 POST 添加 PUT 修改 DELETE 删除

params:用来设置客户端传到服务器的数据,支持表达式

  • username:必须包含username参数
  • !username:不能包含username参数
  • username = admin :username必须等于admin
  • username != admin :username必须不等于admin

headers:用来设置请求头信息,所发送的请求的请求头信息一定要和headers属性中所设置的一致

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Controller()
public class TestServlet {
    @RequestMapping(value = "/some.do")
    public ModelAndView doSome(){
        //表示本次请求的处理结果
        //Model:数据,请求处理完成后,要显示给用户
        //View:视图,比如jsp等
        ModelAndView mv = new ModelAndView();
        //添加数据,底层执行request.setRequestContext();
        mv.addObject("msg","欢迎使用SpringMVC做web项目");
        mv.addObject("fun","执行doSome方法");
        //指定视图,指定视图的完整路径
        //框架对视图执行forward操作
        mv.setViewName("show");
        return mv;
    }
}

第六步:创建主页面和定义目标页面

?
1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<a href="some.do">发起some.do的请求</a>
</body>
</html>
?
1
2
3
4
5
6
7
8
9
10
11
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
SpringMVC的第一个项目
<h3>msg数据:${msg}</h3><br/>
<h3>fun数据:${fun}</h3>
</body>
</html>

使用SpringMVC框架web请求处理顺序

springMVC详细介绍

总结

本篇文章就到这里了,希望可以帮助到你,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.****.net/qq_45796208/article/details/112697210