• springboot(二 如何访问静态资源和使用模板引擎,以及 全局异常捕获)


    在我们开发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类型。不然会一直找不到页面,这里不记录

  • 相关阅读:
    新建立了个集邮 Blog
    删除8848的mysearch
    VS2005的中国发布会
    免费的PDF生成工具
    FreeBASIC
    今天收到了WinZip发来的免费License
    简洁的 Bash 编程技巧
    benhuan039sblog.wordpress.20121111.xml_.txt
    新浪微博除掉推荐微博
    自制力也是一种力量
  • 原文地址:https://www.cnblogs.com/920913cheng/p/10303496.html
Copyright © 2020-2023  润新知