SpringBoot中关于static和templates的注意事项以及webjars的配置

时间:2021-11-18 03:24:54

1. 默认情况下, 网页存放于static目录下, 默认的"/"指向的是~/resouces/static/index.html文

2. 如果引入了thymeleaf, 则默认指向的地址为~/resouces/templates/index.html

?
1
2
3
4
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-thymeleaf</artifactid>
</dependency>

代码结构:

SpringBoot中关于static和templates的注意事项以及webjars的配置

3.在引入thymeleaf后, 如果仍需要访问~/static/index.html, 则可以使用重定向

?
1
return "redirect:/index.html"

代码样例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestmapping;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
@controller
public class homectrl {
  @getmapping("/")
  public string homepage(model model, httpservletrequest request, httpservletresponse response) throws ioexception {
    return "/index";
  }
  @requestmapping("/static")
  public string navigatortostatic() {
    return "redirect:/static.html";
  }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!doctype html>
<html>
<head>
  <script src="webjars/jquery/3.1.1/jquery.min.js"></script>
  <script src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" />
</head>
<body>
<div class="container"><br/>
  <div class="alert alert-success">
    hello, <strong>bootstarp & webjars!</strong>
  </div>
</div>
</body>
</html>

4. html中引入webjars时, 需导入类似下面的包

?
1
2
3
4
5
6
7
8
9
10
<dependency>
 <groupid>org.webjars</groupid>
 <artifactid>bootstrap</artifactid>
 <version>3.3.7</version>
</dependency>
<dependency>
 <groupid>org.webjars</groupid>
 <artifactid>jquery</artifactid>
 <version>3.1.1</version>
</dependency>

5. html样例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!doctype html>
<html>
<head>
  <script src="webjars/jquery/3.1.1/jquery.min.js"></script>
  <script src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" />
</head>
<body>
<div class="container"><br/>
  <div class="alert alert-success">
    hello, <strong>bootstarp & webjars!</strong>
  </div>
</div>
</body>
</html>

6. 结果:

SpringBoot中关于static和templates的注意事项以及webjars的配置

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/sanpic/article/details/79519070