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";
    }
}

结果

相关推荐
程序猿小D35 分钟前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
CodeWithMe1 小时前
【Note】《深入理解Linux内核》 Chapter 15 :深入理解 Linux 页缓存
linux·spring·缓存
llwszx2 小时前
Spring中DelayQueue深度解析:从原理到实战(附结构图解析)
java·后端·spring·delayqueue·延迟任务
C182981825753 小时前
OOM电商系统订单缓存泄漏,这是泄漏还是溢出
java·spring·缓存
hello早上好4 小时前
JDK 代理原理
java·spring boot·spring
何苏三月5 小时前
SpringCloud系列 - Sentinel 服务保护(四)
spring·spring cloud·sentinel
纳兰青华5 小时前
bean注入的过程中,Property of ‘java.util.ArrayList‘ type cannot be injected by ‘List‘
java·开发语言·spring·list
魔芋红茶6 小时前
spring-initializer
python·学习·spring
zzywxc7877 小时前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring
llwszx12 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁