【SpringMVC】—— 如何配置使用SpringMVC(详细步骤)

目录

引言

使用

1、新建模块

2、导入坐标

3、创建SpringMVC控制器类

4、初始化SpringMVC环境

5、初始化Servlet容器,加载SpringMVC环境

6、配置运行


引言

        SpringMVC是一种基于Java实现MVC模型的轻量级Web框架,SpringMVC是表现层(web层)的框架,也是spring框架的一部分,用于表现层功能开发。

使用SpringMVC

1、新建模块

如下

注意:有些人可能出现无src文件的情况,这是因为下载太慢!

如何解决上述问题?引入国内镜像

setting.xml文件替换为以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <mirrors>

        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>

        <mirror>
            <id>uk</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://uk.maven.org/maven2/</url>
        </mirror>

        <mirror>
            <id>CN</id>
            <name>OSChina Central</name>
            <url>http://maven.oschina.net/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>

        <mirror>
            <id>nexus</id>
            <name>internal nexus repository</name>
            <url>http://repo.maven.apache.org/maven2</url>
            <mirrorOf>central</mirrorOf>
        </mirror>

        <!-- junit镜像地址 -->
        <mirror>
            <id>junit</id>
            <name>junit Address/</name>
            <url>http://jcenter.bintray.com/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>

        <mirror>
            <!--This sends everything else to /public -->
            <id>nexus-aliyun</id>
            <mirrorOf>*</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>

        <mirror>
            <id>osc</id>
            <mirrorOf>*</mirrorOf>
            <url>http://maven.oschina.net/content/groups/public/</url>
        </mirror>

        <mirror>
            <id>repo2</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo2.maven.org/maven2/</url>
        </mirror>

        <mirror>
            <id>net-cn</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://maven.net.cn/content/groups/public/</url>
        </mirror>

        <mirror>
            <id>ui</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://uk.maven.org/maven2/</url>
        </mirror>

        <mirror>
            <id>ibiblio</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
        </mirror>

        <mirror>
            <id>jboss-public-repository-group</id>
            <mirrorOf>central</mirrorOf>
            <name>JBoss Public Repository Group</name>
            <url>http://repository.jboss.org/nexus/content/groups/public</url>
        </mirror>

        <mirror>
            <id>JBossJBPM</id>
            <mirrorOf>central</mirrorOf>
            <name>JBossJBPM Repository</name>
            <url>https://repository.jboss.org/nexus/content/repositories/releases/</url>
        </mirror>

    </mirrors>
</settings>

2、导入坐标

在pom.xml文件中导入springmvc和servlet。

注意刷新maven

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

3、创建SpringMVC控制器类

用于处理请求。

@Controller设定SpringMVC的核心控制器bean,定义为bean

@RequestMapping设置当前控制器方法请求访问路径

@ResponseBody设置当前控制器方法,响应内容为当前返回值,返回给前端

@Controller
public class UserController {
    @RequestMapping("/save")
    @ResponseBody
    public String save(){
        System.out.println("user save ...");
        return "{'module':'springmvc'}";
    }
}

4、初始化SpringMVC环境

加载对应的bean

@Configuration
@ComponentScan("com.spring.controller")
public class SpringMvcConfig {
}

5、初始化Servlet容器,加载SpringMVC环境

public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {

    //加载springMVC容器配置
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMvcConfig.class);
        return ctx;
    }

    //设置哪写请求归属springMVC处理
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //加载spring容器配置
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }

6、配置运行

pom.xml文件需要有以下内容:

配置tomcat,jdk版本为1.8

  <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>
  </properties>

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

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>80</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>

        运行程序,浏览器输入http://localhost/save,打印出save()方法return内容即成功,后端控制台打印出save()方法输出内容。


  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四月天行健

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值