使用springboot响应静态页面,以及重定向

需求:
根据用户访问的uri给用户响应对应的名片网页
在这里插入图片描述

在这里插入图片描述

方案一:

如图,再resource下创建static,并将页面放入其中
如果名称为index则默认为该端口的'/'路径访问的文件,如果想访问指定的页面则需要路径重指定文件名称
在这里插入图片描述

方案二:

回到需求重,需求需要根据用户访问的uri经行重定向,这里我们就用到了
redirect:index.html

// 这里不要使用responsebody或者restController,否则将不会通过视图解析器进行响应
@Controller
public class TestController {
    @RequestMapping("/personal")
    public String get(){
        System.out.println("到达");
        //  跳转到当前路由的index.html
        return "redirect:index.html";
    }
    @RequestMapping("/personal2")
    public String get2(){
        System.out.println("到达");
        return "index.html";
    }
    @RequestMapping("/getPay")
	public String get() {
    System.out.println("ok");
    // 跳转到根目录开始的index.html
    return "redirect:/index.html";
}
}

此时就可以根据用户访问的路径进行重定向;

方案三

用springmvc的配置

spring:
  mvc:
    view:
      suffix: .html
    static-path-pattern: /**
  resources:
    static-locations: classpath:/templates/,classpath:/static/

方案四:

使用Thymeleaf模板引擎
依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.1.6.RELEASE</version>
</dependency>
@Controller
public class TestController {

    @RequestMapping("/personal")
    public String get(){
        System.out.println("到达");
        return "index";
    }
}

注意:页面必须放在templates文件夹下,否则找不到,注意文件夹结构,编译以后和com同层
在这里插入图片描述
在这里插入图片描述

当然也可以修改配置文件,为我们想要的目录

server:
  port: 8080
spring:
  thymeleaf:
    prefix: classpath:/static/
    suffix: .html #配置后缀名,配置以后可以省略文件后缀
    cache: false #关闭缓存

更改prefix对应的值可以改变Thymeleaf所访问的目录。但好像只能有一个目录。

综上:模板引擎的使用与否都可以实现页面的访问。区别在于页面所存放的位置以及访问或返回的时候后缀名加不加的问题
在这里插入图片描述
此时我们就可以直接响应文件名作为视图;

阴间方案五

@GetMapping("/t/{path}")
public void getPage(@PathVariable String path) throws IOException {
    if ("favicon".equals(path))
        return;
    path = StringUtils.isBlank(path) ? "Device App.html" : path + ".html";
    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/" + path);
    assert stream != null;
    ByteBuffer buf = ByteBuffer.allocate(stream.available());
    Channels.newChannel(stream).read(buf);
    buf.flip();
    String html = Charset.defaultCharset().decode(buf).toString();
    buf.clear();
    response.setContentType("text/html;charset=utf-8");
    response.getWriter().write(html);
}

请求转发补充

@RequestMapping("/download/{md5}")
public String download(@PathVariable String md5){
    System.out.println("下载文件:"+md5);
    return "forward:/d/"+md5;
}
@RequestMapping("/d/{md5}")
public String d(@PathVariable String md5){
    System.out.println("下载文件:"+md5);
    return "redirect:https://adrrr.ssss-cn-beijing.aliyuncs.com/restsrLogo.png";
}

这样在访问上面download的时候就可以对请求进行转发,转发到下面的路由去。路由不包括前缀

请求重定向

 @GetMapping(value = "/playSectionAudio")
    public ModelAndView playSectionAudio(@RequestParam String fileUrl) throws ClientException {
        URL url = aliYunSTSUtils.getPrivateOssResourse(fileUrl);
        return new ModelAndView("redirect:" + url);
    }

如果加了@RestController或者@ResponseBody则需要指定返回值为ModelAndView
异常解决
在这里插入图片描述

@Bean
public InternalResourceViewResolver viewResolver(){
    return new InternalResourceViewResolver();
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值