Spring MVC入门案例

作者 : 开心源码 本文共3237个字,预计阅读时间需要9分钟 发布时间: 2022-05-12 共196人阅读

作为Spring非常重要的一个组件,Spring MVC在java web领域已经是当之无愧的领头人物,能正确的使用Spring MVC非常重要,今天就一起搭建一个Spring MVC项目的骨架,一起入门下Spring MVC。

软件

  • Spring 4.x
  • Tomcat 8.x
  • MacOS(不限,Java是跨语言的)

案例

  1. 新建maven项目,名称可以任意起。我们的就叫做me.aihe.learnspring

    image

  2. 增加Spring与spring mvc的依赖。

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>me.aihe</groupId>    <artifactId>learnspring</artifactId>    <version>1.0-SNAPSHOT</version>    <properties>        <spring.version>4.3.14.RELEASE</spring.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.version}</version>        </dependency>    </dependencies>    </project>
  1. 配置项目为tomcat项目,记得提前下载好tomcat软件,我们通过idea指定tomcat的位置。

image

  1. 通过idea配置项目为web项目

image

  1. 配置Java Web项目的web.xml文件。
<?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>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcher</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>
  1. 配置dispatcher-servlet.xml,tomcat启动的时候会加载servlet名称-servlet.xml的文件。所以我们要在WEB-INF下再增加一个文件dispatcher-servlet.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">    <mvc:annotation-driven />    <context:component-scan base-package="me.aihe" /></beans>
  1. 创立一个新的Controller来测试。
package me.aihe;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * description:  该文件说明 * * @author aihe * @version 1.0 * @date 2018/11/6 */@Controllerpublic class FirstController {    @RequestMapping({"/","/index"})    @ResponseBody    public String index(){        return "index";    }}
  1. 再次确认下,项目能否为正确的web项目,idea项目启动的包能否正确,比照下面的图片。先按照下面的进行配置,每个包的细节后面再说。

imageimage

  1. 启动项目,可以看到正确的项目输出。浏览器上看到页面是正常的工作。

imageimage

  1. 到这里,我们的Spring MVC基本项目就搭建完成了,再后续开发的时候,需要不断的增加依赖让项目变得越来越复杂,但是复杂的项目也是由简单的项目一步步来的,基础的东西掌握扎实也很重要。

Spring Boot的web案例

鉴于Spring Boot构建一个web项目非常的简单快速,这里顺便提及一下,而且近些年Spring Boot越来越火,掌握一下也是很有必要的。

  1. 基本上现在的IDE都会有Spring Boot的initializer. 我们用的是Idea,如图。

    image

  2. 勾选web依赖即可以。

image

  1. 直接创立一个Controller的java类即可以。

image

  1. 启动项目

image

是不是非常的简单,Spring Boot极大的简化了我们开发的步骤,但是想要提高技术最好还是要明白其内部的原理。

最后

这次关于Spring MVC的环境调试就说到这,本次主要详情了使用Tomcat搭建Spring MVC项目与使用Spring Boot搭建web项目。

假如想对Spring MVC有更深入的了解,参考之前写的少量关于Spring MVC原理分析的文章
探究Spring

参考

  • 探究Spring

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Spring MVC入门案例

发表回复