SpringBoot请求方式和不同包访问静态页面

配置文件aplication.properties

  1. #设置为公共访问的包

  2. spring.resources.static-locations=classpath:/resources/,classpath:/static/,classpath:/page/

Spring Boot 请求方式:

1.get请求

@RequestMapping(value = "/findUser",method = RequestMethod.GET)
    public UserInfo findUser(){
        return new UserInfo("1","张三","123","男","22");
    }


2.restful风格

@GetMapping(value = "/rest/{id}/{name}")
    public Map findObject(@PathVariable(value = "id") String id, @PathVariable(value = "name") String name){
        map.clear();
        map.put("id",id);
        map.put("name",name);
        return map;
    }


3.分页列表

    @GetMapping(value = "/page")
    public Map pages(@RequestParam(name = "page",defaultValue = "1")Integer page,Integer limit){
        map.clear();
        map.put("page",page);
        map.put("limit",limit);
        return map;
    }


4.添加数据

 

    @PostMapping(value = "/add")
    public Map addUser(UserInfo ui){
        map.clear();
        map.put("ui",ui);
        return map;
    }


5.token形式

    @GetMapping(value = "/findheader")
    public Map findHeader(@RequestHeader("token")String token,String id){
        map.clear();
        map.put("token",token);
        map.put("id",id);
        return map;
    }


6.查找信息

    @GetMapping(value = "/find")
    public Map findHeader(HttpServletRequest request){
        map.clear();
        String id = request.getParameter("id");
        map.put("id",id);
        return map;
    }


7.登录

    @PostMapping(value = "/login")
    public Map login(String name ,String pwd){
        map.clear();
        map.put("name",name);
        map.put("pwd",pwd);
        return map;
    }


8.修改信息

    @PutMapping(value = "/update")
    public Map update(String name){
        map.clear();
        map.put("name",name);
        return map;
    }


9.删除信息

    @DeleteMapping(value = "/delete")
    public Map delete(String id){
        map.clear();
        map.put("id",id);
        return map;
    }


常用框架阿里fastjson, 谷歌gson

JavaBean序列化为Json,性能: Jackson > FastJson > Gson > Json-lib 同个结构

Jackson、FastJson、 Gson类库各有优点,各有自己的专长

空间换时间,时间换空间

Jackson处理相关自动

指定字段不返回: @JsonIgnore

指定日期格式: @JsonFormat (pattern="yyy-MM-dd hh:mm:ss" , locale=" zh" , timezone="GMT+8" )

空字段不返回: @JsonInclude(Include . NON_NULL)

指定别名: @JsonProperty

SpringBoot目录文件结构

1、目录讲解

src/main/java:存放代码

src/main/resources

        static:存放静态文件,比如css、 js、 image, (访问方式 http://localhost:8080/js/xxx.js)

        templates:存放静态页面jsp, html,tpl

        config:存放配置文件, application. properties

        resources:

2.引入依赖 Thymelesf

       

<dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>


如果不引人这个依赖包,html文件应该放在默认加载文件夹里面,比如resources、static. public这个几个文件夹,才可以访问。

3、同个文件的加载顺序,静态资源文件

Spring Boot 默认会挨个从META/resources > resources > static > public里面找是 否存在相应的资

源,如果有则直接返回。

4、默认配置

1) spring.resources. static-locations = classpath: /META- INF/resources/ , classpath: /resources/ ,classpath:/static/ , classpath:/public/

Spring Boot访问静态资源

在浏览器中直接输入 localhost:8080/a.html 会去访问 static 文件夹下的静态资源

如果想要访问到 templates 下的静态资源有两种方式,但是都要在pom.xml里面去引入Thymelesf依赖。

1)在application.properties里面添加以下配置定死

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
2)在application.properties里面添加以下配置进行灵活访问

spring.resources.static-locations = classpath:classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/page/
最后在Controller控制层中进行访问

package org.zzp.bootdemo.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
 
@Controller
public class TestController {
 
    @GetMapping("/finds")
    public String find(){
        return "a";
    }
 
    @GetMapping("/findss")
    public String finds(){
        return "ab.html";
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值