开发控制器controller
开发控制器controller
涉及注解:
@controller
@RequestMapping
1、开发controller页面
Spring Boot可以整合Spring MVC、Spring WebFlux、Struts 2 等各种前端MVC框架。
如何开发控制器则取决于用哪种MVC框架。
如Spring MVC,则依然使用如下两个注解:
@Controller (是@Component的变体)
@RequestMapping(@GetMapping、@PostMapping、@PutMapping等)
具体代码
@Controller
public class BookController {
@GetMapping("/index")
public String index(Model model){
//把数据映射到页面
model.addAttribute("controllerPage","第一个springboot页面");
//这里的是逻辑视图名:"first" , 到时创建的物理视图页面,名字也要一致,为“first”
return "first";
}
@GetMapping("/book")
@ResponseBody //该方法是一个Restful API , 该方法的返回值将直接作为响应
public ResponseEntity<Map<String,String>> book(Model model){
//创建书本数据
Map<String, String> map = new HashMap<>();
map.put("name:","火影忍者");
map.put("price:","100");
return new ResponseEntity<>(map, HttpStatus.OK);
}
}
2、为springboot应用添加主类
就是添加一个springboot的启动类,主类用@SpringBootApplication注解修饰。
main方法中只要如下
SpringApplication.run(参数1:被@SpringBootApplication注解修饰的类(配置类), 参数2:args);
具体代码
@SpringBootApplication
public class BootApplication {
public static void main(String[] args) {
//run方法 就是运行被@SpringBootApplication这个注解修饰的类
//运行spring boot项目,启动Spring 容器
SpringApplication.run(BootApplication.class,args);
}
}
3、启动springboot项目,访问controller方法。
访问成功
重点解释:@SpringBootApplication
@SpringBootApplication相当于如下3个注解的组合
注解1:@SpringBootConfiguration: 就是@Configuration,只不过它的proxyBeanMethods属性默认为true。
@Configuration 将被修饰的类变成Java配置类——这就是Spring容器的Java配置方式。
注解2:@EnableAutoConfiguration: 用于开启自动配置。Spring Boot核心注解。
注解3:@ComponentScan:告诉Spring容器要自动扫描哪些类来作为容器中的Bean—— 属于"Spring零配置"部分的知识点。
该注解(@SpringBootApplication)开启了3个功能:
1、将被修饰的类变成了配置类。( @Configuration)
2、开启了零配置的自动扫描。 (@ComponentScan)
默认会自动扫描@SpringBootApplication修饰的类所在包及其子包下的所有组件。
3、开启了自动配置。(@EnableAutoConfiguration)
SpringApplication.run()的两个功能:
1、自动加载该第一个参数所代表的配置类、并创建该配置类中所有Bean,并扫描该配置类相同包中或其子包下的所有Bean。
2、返回所创建的Spring容器
4、添加视图
模版引擎:Thymeleaf
pom.xml 添加视图依赖
<!-- 添加视图模版引擎 thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
添加bootstrap模版
可以在官网找依赖https://mvnrepository.com/
<!-- 添加 bootstrap 依赖 -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.6.0</version>
</dependency>
物理视图页面代码:
<!DOCTYPE html>
<!-- HTML引入Thymeleaf ,导入命名空间 , 把thymeleaf的域名加进来就可以使用了-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>第一个页面</title>
<!-- 导入 webjar 包中的 bootstrap 样式库 -->
<link ref="stylesheet" th:href="@{/webjars/bootstrap/4.6.0/css/bootstrap.css}">
</head>
<body>
<!-- class="container" :相当于类名,就是这个div模块的名字-->
<!-- class="alert alert-primary" : 警报框 -->
<div class="container">
<div th:text="${controllerPage}" class="alert alert-primary">提示信息</div>
</div>
</body>
</html>
Thymeleaf 介绍
Thymeleaf是下一代的视图模板技术,优点在于:用属性代替原来的子元素
——而浏览器可以自动忽略不能处理的属性,因此浏览器可以直接浏览Thymeleaf视图页面。
改变:
为<html…/>元素增引入th:命名前缀,其他元素上使用th:text、th:src、th:href…等属性指定表达式
好处:
浏览器可以直接查看Thymeleaf的模板页面,当然该模板页面也可被模板引擎解析。
Spring Boot支持Webjar技术,
Webjar:允许将各种前端资源(JS\css等打包成JAR包)
位置:
Thymeleaf的模板页面放在resources/templates目录下
注意点:
第一点:
引入这个Thymeleaf的命名空间,才能使用这个模版引擎
<!-- HTML引入Thymeleaf ,导入命名空间 , 把thymeleaf的域名加进来就可以使用了-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
第二点:
导入 webjar 包中的 bootstrap 样式库
因为是添加bootstrap依赖的,所以需要这样引入
第三点:
注意文件夹命名
html页面存放的位置,因为使用的是mvc模式(用@controller注解表示用的是mvc模式),所以这个命名是默认的,用 templates ,如果写错,那么页面就无法拿到后端传来的数据了。如拿不到controllerPage映射的数据。
访问页面,成功
成功从后端获取数据,填充到div中,如果前后端的“controllerPage”名称没有对应上,那么就会获取失败,就不会显示“第一个springboot页面”这个数据