SpringBoot中关于static和templates的注意事项以及webjars的配置
2019-01-31 12:32
916 查看
1. 默认情况下, 网页存放于static目录下, 默认的"/"指向的是~/resouces/static/index.html文
2. 如果引入了thymeleaf, 则默认指向的地址为~/resouces/templates/index.html
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
代码结构:
3.在引入thymeleaf后, 如果仍需要访问~/static/index.html, 则可以使用重定向
return "redirect:/index.html"
代码样例:
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"; }
<!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时, 需导入类似下面的包
<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样例
<!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. 结果:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接
您可能感兴趣的文章:
相关文章推荐
- 关于spring3.2.4与hibernate4.1.2 集成的一些注意事项以及配置
- Spring boot 集成mybatis通用mapper配置步骤及注意事项
- Spring通过properties配置数据库链接以及注意事项
- 关于Androidstudio的安装以及配置时候的注意事项
- 在spring boot中的log4j2编程式配置(Programmatic Configuration)注意事项
- velocity与spring整合的配置、应用以及注意事项
- 关于springboot中的web项目不能访问templates中的静态资源
- 关于springboot 配置文件、Aop配置以及jdbc配置
- spring boot配置duboo注意事项
- 关于常见的变量修饰符: const, static, extern, register, auto 的详细解释,使用场合以及注意事项
- 关于spring boot打包及其注意事项
- 关于Spring Boot WebSocket整合以及nginx配置详解
- Spring及SpringBoot @Async配置步骤及注意事项
- 关于纯Java配置springweb和springboot学习忠告
- springboot静态资源的配置注意事项
- 关于spring配置两个sessionFactory的配置与注意事项
- SpringBoot开发详解(三)--SpringBoot配置文件YML注意事项
- Spring Boot 学习笔记(二)—— WEB相关配置
- 关于maven配置pom.xml的小注意事项(provided)
- Java框架spring Boot学习笔记(二):Hello Spring Boot、以及项目属性配置