Spring MVC 入门例子

Spring Web MVC 是一个建立在 Servlet API 的 Web 框架,它属于 Spring Framework 的产品。Spring Web MVC 名称来源于模块 spring-webmvc ,但它更常用的叫法是 Spring MVC。
本文讲述如何使用 IntelliJ IDEA 来编写一个 Spring MVC Demo。

使用 IntelliJ IDEA 创建一个 Maven 项目,选择 webapp 类型。

这里写图片描述

创建好 Maven 项目后,我们在 pom.xml 文件中添加依赖。

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

为编写代码,我们在 main 目录下创建 java 目录,并标记为 Source 目录。接下来在 main 目录下添加 resources 目录,并标记为 Resource 目录。如下图所示:

这里写图片描述

为运行 Spring MVC 项目,我们还需要配置 tomcat 服务器。 先在官网下载 tomcat,然后在 IntelliJ IDEA 添加 tomcat ,如下图所示:

这里写图片描述

配置完 tomcat 服务器后,还需要将项目部署到 tomcat 中运行。在 Deployment 选项窗口中将本项目配置进去。

这里写图片描述

Tomcat 服务器配置完毕,可以点击运行按钮进行测试。如果可以正常打开浏览器,并显示 Hello World 的字样,说明配置成功。

这里写图片描述

经过以上的配置,Spring MVC 项目的框架就搭建完毕了,接下来就可以编写代码了。

配置 web.xml

我们将 web.xml 配置修改为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
  <display-name>spring-web</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:application-context.xml </param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>example</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>
</web-app>

在 web.xml 文件,我们添加了一个名为example的 servlet,用于处理 /api/ 的请求;并指定application-context.xml作为 spring 的上下文配置文件。

配置 xxx-servlet.xml

配置完 web.xml 后,我们添加 servlet 的 xml 配置。上面我们配置了名为example的 servlet,故我们创建一个example-servlet.xml的文件,其内容如下:

<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.test.controller" />
</beans>

example-servlet.xml文件里面指定了 controller 的包为com.test.controller

添加 controller

上述 servlet 配置文件中指定了 servlet 的包为com.test.controller,因此,我们在 java 目录下创建com.test.controller包,并添加类HelloController

package com.test.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/hello")
public class HelloController {

    @RequestMapping(value = "/spring")
    public void spring(HttpServletResponse response) throws IOException {
        response.getWriter().write("Hello, Spring Web!!");
    }
}

注解 @Controller的含义是指类HelloController为一个处理请求的 Controller。
注解@RequestMapping用于定义请求 url 的映射,上述代码表明我们将 /hello/spring 的请求映射到方法spring()中处理。

完成以上步骤后,我们就完成了一个完整 spring mvc 的示例。项目源代码和配置如下图所示:

这里写图片描述

运行

运行项目,在浏览器中输入http://localhost:8081/,显示结果如下:

这里写图片描述

在浏览器中输入http://localhost:8081/api/hello/spring,显示结果如下:

这里写图片描述

这表明 Spring MVC 框架将 /api/hello/spring 的请求由类HelloController中的spring()方法进行了处理。

参考资料

  1. https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-introduction
  2. https://my.oschina.net/gaussik/blog/385697
  3. https://my.oschina.net/gaussik/blog/513353
  4. https://blog.csdn.net/Mr_Sk/article/details/68947018

猜你喜欢

转载自blog.csdn.net/lihao21/article/details/80726559