Spring MVC (二)

SpringMVC 注解(maven tomcat插件)启动的环境搭建

maven依赖

<!-- 整合springmvc框架依赖  -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.10.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

<!-- json对象-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>2.0.14</version>
    </dependency>

控制(器)层类 

package com.painter.controller;


import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @Author: Painter
 * @project_name: SpringMVC
 * @system_login: sunshine
 * @time: 2022/10/915:52
 */


/**
 * @Controller 标记该类为springMVC控制类
 */
@Controller
public class PainterController {  // 控制类


    /**
     *  访问到该请求返回json数据
     * @return  hashMap (json数据)
     *
     * @RequestMapping ("/get")  url请求映射  和servlet一样
     * @ResponseBody  响应正文
     */
    @RequestMapping("/get")
    @ResponseBody
    public String get(){   // 此方法称之为接口
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("code","200");
        hashMap.put("msg","OK");
//        return "{'code':'200','msg':'OK'}";
        return JSONObject.toJSONString(hashMap);
    }

}

创建配置类

package com.painter.config;

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

/**
 * @Author: Painter
 * @project_name: SpringMVC
 * @system_login: sunshine
 * @time: 2022/10/916:00
 */


/**
 * @Configuration  等同于创建以个xml配置文件
 * @ComponentScan("com.painter.controller")  将com.painter.controller包下的所有类注入到ioc容器中
 *
 * 在springmvc原理 所有请求过来先达到我们的 DispatcherServlet 分发具体控制类 方法执行
 */
@Configuration
@ComponentScan("com.painter.controller")
public class SpringMVCConfig {   // 配置类


}

注册配置类

package com.painter.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

/**
 * @Author: Painter
 * @project_name: SpringMVC
 * @system_login: sunshine
 * @time: 2022/10/916:05
 */


public class ServletInitializer extends AbstractDispatcherServletInitializer {  // 注册配置类
    @Override
    protected WebApplicationContext createServletApplicationContext() {

        AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
        // 注册我们的 springmvc config 配置类
        annotationConfigWebApplicationContext.register(SpringMVCConfig.class);
        return annotationConfigWebApplicationContext;
    }

    @Override
    protected String[] getServletMappings() {

        return new String[]{"/"};
    }

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}

maven tomcat 插件 依赖

  <!-- 配置服务器插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <address>127.0.0.1</address>  <!-- 访问地址 -->
          <port>8080</port>  <!--访问端口号-->
          <path>/</path>  <!-- 应用程序上下文 也是访问路径根路径-->
          <ignorePackaging>true</ignorePackaging>
        </configuration>
      </plugin>
    </plugins>
  </build>

添加运行配置

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_painter

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

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

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

打赏作者

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

抵扣说明:

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

余额充值