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

结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值