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

结果

相关推荐
克拉克盖博41 分钟前
chapter03_Bean的实例化与策略模式
java·spring·策略模式
小兔兔吃萝卜6 小时前
Spring 创建 Bean 的 8 种主要方式
java·后端·spring
AAA修煤气灶刘哥7 小时前
面试官: SpringBoot自动配置的原理是什么?从启动到生效,一文讲透
后端·spring·面试
qq_三哥啊9 小时前
【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
spring·intellij-idea·mybatis
别惹CC10 小时前
Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
人工智能·spring boot·spring
寒士obj10 小时前
Spring事物
java·spring
IT毕设实战小研18 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
甄超锋19 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
Java小白程序员1 天前
Spring Framework:Java 开发的基石与 Spring 生态的起点
java·数据库·spring
甄超锋1 天前
Java Maven更换国内源
java·开发语言·spring boot·spring·spring cloud·tomcat·maven