简化spring应用的处死搭建以及开发过程
创建springboot如果卡住了可以按照红色的改
勾三个就可以了
1.先创建,一个user含有name,age‘属性的get(),set()方法。
2.创建一个class文件TestController,代码如下
@Controller
public class TestController {
//简化使用spring springMVC
//包括响应头 和body
// 接口请求返回数据类型
//1.html 2.String-》json
@RequestMapping("test")
@ResponseBody
public User getString(){
//对象转化为String(json)
//集成了json依赖包 自动转换
User user =new User();
user.setAge(20);
user.setName("zhangsan");
return user;
}
3.运行之后,浏览器的目录名为http://localhost:8080/test就可以出现Json接口。
所谓热更新就是不需要重新启动服务,只需要更新代码就能显示出来。
1、首先进行配置,点击setting,按如图勾选。
2、再将资源实时更新,否则就无用了
3、再将项目重启。
在application.properties文件里进行编写代码,注意不能改文件名,否则就找不到路径。
server.port=8088 server.servlet.context-path=/springboot
在浏览器中localhost:8088/springboot/test就完成了。
最后显示
1.在application.properties文件里进行编写代码
server.port=8088 server.servlet.context-path=/springboot spring.web.resources.static-locations=classpath:/templates
2.再将图片复制到templates目录里。
3.在浏览器中输入http://localhost:8088/springboot/img.png,img.png是照片名。
server.port=8088 server.servlet.context-path=/springboot spring.web.resources.static-locations=classpath:/templates,file:d:/imgs/1.png模板Thymeleaf
先加入依赖包
简单参数org.springframework.boot spring-boot-starter-thymeleaf
1.在TestController文件里编写以下代码
@RequestMapping("gethtml")
public String gethtml(Model model){
model.addAttribute("name","test1234");
return "test.html";
}
2.在test.html里编写代码,注意name对应TestController里的addAttribute第一个参数
在浏览器http://localhost:8088/springboot/gethtml展示出来的如图
1.在TestController文件里编写以下代码,注意*model.addAttribute(“userlist”,userList)*这个和下面的test.html中的参数对应
@RequestMapping("gethtml")
public String gethtml(Model model){
List userList=new ArrayList<>();
for(int i=0;i<10;i++){
User user =new User();
user.setAge(20);
user.setName("zhangsan:"+i);
userList.add(user);
}
model.addAttribute("userlist",userList);
return "test.html";
2.在test.html里编写代码,
在http://localhost:8088/springboot/gethtml中显示



