springboot(二 如何访问静态资源和使用模板引擎,以及 全局异常捕获)
在我们开发Web应用的时候,需要引用大量的js、css、图片等静态资源。
默认配置
Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
/static
/public
/resources
/META-INF/resources
- 举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件。启动程序后,尝试访问http://localhost:8080/D.jpg。如能显示图片,配置成功。


2 .springboot 使用freemarker模板引擎,模板引擎的作用:是为了使用户界面与业务数据(内容)分离而产生的。
导入freemarker模板引擎所需要的依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jiahou</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 引入springboot 父类依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<!-- 用于web开发的话 导入 springboot -web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
编写 测试的controller
package com.springboot.hello; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping; // 该注解的作用 是表示 在HelloSpringboot类的所有方法 返回的都是json格式
@Controller
public class Springbootfmk { @RequestMapping("/index1")
public String index(ModelMap map) {// ModelMap转发值的作用
map.addAttribute("name", "springboot使用freemarker");
return "index1";
}
然后 在src/main/resources目录下创建templates文件夹 并且 创建一个名字为index1 后缀是.ftl的文本格式:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
${name}
</body>
</html>

运行结果:
3.全局异常捕获,在程序中不能给用户 返回404 或者500 可以进行统一的处理消息 针对返回json格式
package com.springboot.hello; import java.util.HashMap;
import java.util.Map; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; // @ControllerAdvice 是 controller 的一个辅助类,最常用的就是作为全局异常处理的切面类 (可以指定扫描的范围)
@ControllerAdvice
public class EexceptionController {
// 拦截运行时异常
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Map<String, String> disException() {
Map<String, String> map = new HashMap<String, String>();
map.put("异常处理", "500");
return map;
} }
@RequestMapping("testException")
public void testException() {
System.out.println(1 / 0);
}
运行结果:
4。springboot 一般不推荐使用jsp,不过 要使用jsp 创建springboot的项目的时候 一定要时候war类型。不然会一直找不到页面,这里不记录
springboot(二 如何访问静态资源和使用模板引擎,以及 全局异常捕获)的更多相关文章
- SpringBoot: 5.访问静态资源(转)
springboot默认从项目的resources里面的static目录下或者webapp目录下访问静态资源 方式一:在resources下新建static文件(文件名必须是static) 在浏览器中 ...
- springboot+themeleaf+bootstrap访问静态资源/无法访问静态资源/图片
在网页HTML上访问静态资源的正确写法例: 1.<img src="../../static/bootstarp/img/2.jpg" th:src="@{ ...
- springboot程序无法访问静态资源
今天开发遇到了一个很奇葩的错误,再spngboot程序成功运行后发现无法访问再resouces/static下的静态资源,通过rul访问总是404,原因最终锁定在某配置类的一个标签上: @Enable ...
- SpringBoot学习5:访问静态资源
springboot默认从项目的resources里面的static目录下或者webapp目录下访问静态资源 方式一:在resources下新建static文件(文件名必须是static) 在浏览器中 ...
- springboot 2.X 在访问静态资源的的时候出现404的问题
通过idea快速搭建一个springboot项目: springboot版本2.1.6 在网上看的资料,springboot静态资源访问如下: "classpath:/META‐INF/re ...
- Springboot访问静态资源&WebJars&图标&欢迎页面
目录 概述 1.访问WebJar资源 2.访问静态资源 3.favicon.ico图标 4.欢迎页面 概述 使用Springboot进行web开发时,boot底层实际用的就是springmvc,项目中 ...
- springBoot怎样访问静态资源?+静态资源简介
1.静态资源 怎样通过浏览器访问静态资源? 注意:不需要加static目录.因为只是告诉springboot目录,而不是静态资源路劲. 这时访问路径就需要加上/static
- 解决SpringBoot页面跳转无法访问静态资源的问题
初学SpringBoot,写项目的时候遇到了问题,原本的页面是这样的 但启动项目后是这样的 这是因为thymeleaf中引入静态资源及模板需要使用到 th:xxx 属性,否则无法在动态资源中访问静态资 ...
- 【SpringBoot】06.SpringBoot访问静态资源
SpringBoot访问静态资源 1.SpringBoot从classpath/static的目录 目录名称必须是static 启动项目,访问http://localhost:8080/0101.jp ...
随机推荐
- 启动tomcat报错:Failed to start component [StandardEngine[Catalina].StandardHost[localhost]
1.右键点击需要启动的tomcat,选择Clean和Clean Tomcat Work Directory,清除即可!
- Linux安装模式AppImage,Flatpak,Snap整理
本文只谈Linux世界用户较多的前2大主要分支, RedHat Red Hat Enterprise Linux 简称RHEL rpm (RedHat, CentOS, Fedora, Oracle. ...
- Python语言规范
Lint 对你的代码运行pylint 定义: pylint是一个在Python源代码中查找bug的工具. 对于C和C++这样的不那么动态的(译者注: 原文是less dynamic)语言, 这些bug ...
- IDEA 热部署- 自动编译设置
原文:https://www.cnblogs.com/TechSnail/p/7690829.html && https://blog.csdn.net/qq_3129357 ...
- using python to compute production rules
#coding=utf8 import loggingimport itertoolsimport reimport sys logger = logging.getLogger()root_form ...
- 调试 shell script 方法
wade@V1088:~$ cat b.sh#!/bin/bash dir=`pwd` dir=$dir'/' for f in `ls *.png` do echo $dir$f done 看每一行 ...
- java 类方法和实例方法 以及 类变量和实例变量 各种区别
我们已经知道类体中的方法分为实例方法和类方法两种,用static修饰的是类方法.二者有什么区别呢?当一个类创建了一个对象后,这个对象就可以调用该类的方法. 当类的字节码文件被加载到内存时,类的实例方法 ...
- LG3809 【模板】后缀排序
题意 题目背景 这是一道模板题. 题目描述 读入一个长度为 $ n $ 的由大小写英文字母或数字组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的 ...
- mac 终端光标在单词之间移动
https://ruby-china.org/topics/1241#reply16 you can use : option + b / f not ctrl + <- / ->
- nginx日志格式配置
我一向对日志这个东西有些许恐惧,因为在分析日志是需要记住不同服务器日志的格式,就拿提取ip这一项来说,有的服务器日志是在第一列,有的是第二列或则第三列等等.知道今天我才发现,日志格式是可以自定义配置的 ...