SpringMvc

mvc思想

前端-controller - service - mapper - 数据库

view - controller - model

创建Springmvc入门案例

1.创建模块,导入坐标

复制代码
<!-- 因为是tomcat方式所以打包方式用war包 -->
<packaging>war</packaging>
<!-- 集成依赖 -->
<dependencies>
    <!-- servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
           <!-- servlet包在tomcat中冲突 给他划定依赖范围-->
        <scope>provided</scope>
    </dependency>
    <!-- spring-mvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
    <!-- 版本很容易出现问题 -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.0</version>
    </dependency>
</dependencies>
<build>
    <plugins>
      <!-- 配置插件 -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <!-- 端口号 -->
                <port>8081</port>
                <!-- 项目虚拟路径 -->
                <path>/ssm</path>
                <!-- get请求参数乱码 -->
                <uriEncoding>utf8</uriEncoding>
            </configuration>
        </plugin>
    </plugins>
</build>

下载插件,maven help 还有tomcat

2.新建包和类

3.新建config配置类

SpringConfig

复制代码
package com.heima.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

//扫描包
@ComponentScan("com.heima")
//注明config配置类
@Configuration
public class SpringConfig {

}

SpringMvcConfig

复制代码
package com.heima.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

//写入注解
@Configuration
@ComponentScan("com.heima.controller")
public class SpringMvcConfig {
}

4.新建初始化启动类

复制代码
package com.heima.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//请求被spring容器管理
@Controller
//请求路径为user
@RequestMapping("/user")
//返回给前端
@ResponseBody
public class UserController {
    //定义功能,写入注解
    @RequestMapping(value = "/login",produces = "text/html;charset=utf-8")
    public String login(HttpServletRequest req, HttpServletResponse res){
        res.setContentType("text/html;charset=utf-8");
        System.out.println("2022103718 张红");
        return "张红 2022103752";
    }
}

结果

相关推荐
Flittly1 天前
【AgentScope Java新手村系列】(11)中断与恢复
java·spring boot·spring
dunky1 天前
Spring 的三级缓存与循环依赖
后端·spring
码云数智-园园6 天前
C++20 Modules 模块详解
java·开发语言·spring
咖啡八杯6 天前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
Flittly6 天前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring
李少兄6 天前
从原理到实战:Spring IoC/DI 核心知识体系与高频面试题全解
java·后端·spring
shushangyun_6 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
ofoxcoding6 天前
在AI API聚合平台配置DeepSeek V3.2提示词缓存实战:快速接入与成本优化指南
人工智能·spring·缓存·ai
一杯奶茶¥6 天前
水果销售网站 CRM客户信息管理系统 超市管理系 酒店管理系统 健身房管理系统 在线音乐网站 校园招聘系统
java·vue.js·spring boot·mysql·spring·java项目
摇滚侠6 天前
SpringMVC 入门到实战 RESTFul 49-55
java·开发语言·后端·spring·intellij-idea·restful