在我们开发Web应用的时候,需要引用大量的js、css、图片等静态资源。

默认配置

Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:

/static

/public

/resources

/META-INF/resources

  1. 举例:我们可以在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(二 如何访问静态资源和使用模板引擎,以及 全局异常捕获)的更多相关文章

  1. SpringBoot: 5.访问静态资源(转)

    springboot默认从项目的resources里面的static目录下或者webapp目录下访问静态资源 方式一:在resources下新建static文件(文件名必须是static) 在浏览器中 ...

  2. springboot+themeleaf+bootstrap访问静态资源/无法访问静态资源/图片

    在网页HTML上访问静态资源的正确写法例: 1.<img src="../../static/bootstarp/img/2.jpg"     th:src="@{ ...

  3. springboot程序无法访问静态资源

    今天开发遇到了一个很奇葩的错误,再spngboot程序成功运行后发现无法访问再resouces/static下的静态资源,通过rul访问总是404,原因最终锁定在某配置类的一个标签上: @Enable ...

  4. SpringBoot学习5:访问静态资源

    springboot默认从项目的resources里面的static目录下或者webapp目录下访问静态资源 方式一:在resources下新建static文件(文件名必须是static) 在浏览器中 ...

  5. springboot 2.X 在访问静态资源的的时候出现404的问题

    通过idea快速搭建一个springboot项目: springboot版本2.1.6 在网上看的资料,springboot静态资源访问如下: "classpath:/META‐INF/re ...

  6. Springboot访问静态资源&WebJars&图标&欢迎页面

    目录 概述 1.访问WebJar资源 2.访问静态资源 3.favicon.ico图标 4.欢迎页面 概述 使用Springboot进行web开发时,boot底层实际用的就是springmvc,项目中 ...

  7. springBoot怎样访问静态资源?+静态资源简介

    1.静态资源 怎样通过浏览器访问静态资源? 注意:不需要加static目录.因为只是告诉springboot目录,而不是静态资源路劲. 这时访问路径就需要加上/static

  8. 解决SpringBoot页面跳转无法访问静态资源的问题

    初学SpringBoot,写项目的时候遇到了问题,原本的页面是这样的 但启动项目后是这样的 这是因为thymeleaf中引入静态资源及模板需要使用到 th:xxx 属性,否则无法在动态资源中访问静态资 ...

  9. 【SpringBoot】06.SpringBoot访问静态资源

    SpringBoot访问静态资源 1.SpringBoot从classpath/static的目录 目录名称必须是static 启动项目,访问http://localhost:8080/0101.jp ...

随机推荐

  1. C语言--第六周作业评分和总结(5班)

    作业链接:https://edu.cnblogs.com/campus/hljkj/CS2017-5/homework/1250 一.评分要求 要求1 完成PTA第六周所有题,若存在抄袭现象,倒扣此题 ...

  2. linux rpm yum 安装 软件

    rpm 安装: 1.rpm包的了解:  rpm  安装  升级  删除 rpm -ivh  ****.rpm   安装 rpm -Uvh  ****.rpm  升级 rpm -e name    删除 ...

  3. HNOI2019 简要题解

    HNOI 2019 简要题解 没想到自己竟也能有机会写下这篇题解呢. LOJ Luogu Day1T1 鱼 枚举\(AD\)两点后发现\(BC\)与\(EF\)相对独立,因此只需要计算合法的\(BC\ ...

  4. 【java编程】Java魔法类:Unsafe应用解析

    转载来源:https://tech.meituan.com/2019/02/14/talk-about-java-magic-class-unsafe.html 前言 Unsafe是位于sun.mis ...

  5. 截断文件函数truncate和ftruncate

    两个函数目的都是将文件大小设置为length参数指定的值 int truncate(const char *pathname,off_t length)//pathname就是路径 int ftrun ...

  6. Modern Data Lake with Minio : Part 1

    转自:https://blog.minio.io/modern-data-lake-with-minio-part-1-716a49499533 Modern data lakes are now b ...

  7. Zabbix-2.4-安装-2

    zabbix自定义报警-动作   打开资产自动接收   这里看到主机资产有数据了,这里的数据,就是来自下面的关联 上面的数据就是设置login-user时候设置的关联    有些关联显示的慢,比如下面 ...

  8. 论 大并发 下的 乐观锁定 Redis锁定 和 新时代事务

    在 <企业应用架构模式> 中 提到了 乐观锁定, 用 时间戳 来 判定 交易 是否有效, 避免 传统事务 的 表锁定 造成 的 瓶颈 . 在 现在的 大并发 的 大环境下, 传统事务 及其 ...

  9. pip in windows

    G:\Python35-32\Scripts>pip install FlaskFatal error in launcher: Unable to create process using ' ...

  10. js正则表达式只能是数字、字母或下划线

    //只能是数字.字母或下划线 function isValid(str) { var reg = /^\w+$/g; return reg.test(str); }