SSM框架-day12-Spring Boot+SpringMVC

Spring Boot

Spring Boo设计目的是用来简化新Spring应用的初始搭建以及开发过程。
1,创建Spring Boot项目
SSM框架-day12-Spring Boot+SpringMVC
SSM框架-day12-Spring Boot+SpringMVC
SSM框架-day12-Spring Boot+SpringMVC
SSM框架-day12-Spring Boot+SpringMVC
SSM框架-day12-Spring Boot+SpringMVC
–2,配置maven
SSM框架-day12-Spring Boot+SpringMVC
SSM框架-day12-Spring Boot+SpringMVC
SSM框架-day12-Spring Boot+SpringMVC
–4,在服务器添加资源
SSM框架-day12-Spring Boot+SpringMVC

package cn.tedu.ideastringboot2;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//@Controller
//@ResponseBody
@RestController//相当于上面两个注解的作用:接收客户端的请求
@RequestMapping("jj")//规定浏览器访问类的方式
public class BootTest {
    //地址栏按照固定方式访问: http://localhost:8080/jj/k.o
    @RequestMapping("k.o")//规定浏览器访问方法的方式
    //注解值不能相同,否则已经存在报错
  public String hello(){
      return "hello spring boot~";
  }
}

测试
关闭之前开启的服务器,再重新运行重启服务器,防止端口号:8080被之前的占用
成功:
SSM框架-day12-Spring Boot+SpringMVC
失败:
Action:

Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port.
SSM框架-day12-Spring Boot+SpringMVC
SSM框架-day12-Spring Boot+SpringMVC

二,SpringMVC

1.核心作用

SpringMVC框架的核心作用:接受浏览器发来的请求 + 给浏览器做出响应

*2.MVC设计模式

好处:实现分层的思想,达到松耦合的目的。提高代码的复用性,扩展性,容错性
M:Model模型,用来封装数据
V:View视图,用来展示数据
C:Controller控制器,用来接受请求和处理响应
SSM框架-day12-Spring Boot+SpringMVC

3.工作原理

前端控制器:DispatcherSerclet,用来接收请求分发请求
处理器映射器:HandlerMapping,用来根据url找具体类里的具体方法
http://localhost:8080/abc/hi
@RequestMapping(“abc”)public class HelloBoot {@RequestMapping(“hi”) public String hello(){ } }
处理适配器:HandlerAdaptor,用来执行方法里的代码
视图解析器:ViewResolever,定位页面的路径
视图:View,用来展示数据
SSM框架-day12-Spring Boot+SpringMVC

4.创建步骤

1.创建module
访问链接: http://localhost:8080/car/get
得到JSON数据: {“id”:718,“name”:“保时捷”,“type”:“Cayman T”,“color”:“红色”,“price”:641000.0}
SSM框架-day12-Spring Boot+SpringMVC
SSM框架-day12-Spring Boot+SpringMVC
–2,创建启动类
SSM框架-day12-Spring Boot+SpringMVC

package cn.tedu.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication//这是一个启动类
public class RunApp {
    public static void main(String[] args) {
        SpringApplication.run(RunApp.class);//运行指定文件
    }
}

–3,创建资源
SSM框架-day12-Spring Boot+SpringMVC

package cn.tedu.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController//接收请求
@RequestMapping("car")
public class CarController {
    @RequestMapping("get")
    public String get(){
        System.out.println(10000);
        return "加油干!";
    }
}

SSM框架-day12-Spring Boot+SpringMVC
SSM框架-day12-Spring Boot+SpringMVC
SSM框架-day12-Spring Boot+SpringMVC

上一篇:SpringMvc学习资料


下一篇:结果跳转处理