Spring boot Sample 014之spring-boot-error-page

2020-12-27 19:28

阅读:437

标签:user   strong   net   静态   char   测试   unit   prope   mave   

一、环境

1.1、Idea 2020.1

1.2、JDK 1.8

二、目的

spring boot 异常处理页面实现方式

三、步骤

3.1、点击File -> New Project -> Spring Initializer,点击next

技术图片

 

3.2、选择Web依赖,选中Spring Web。可以选择Spring Boot版本,本次默认为2.2.6,点击Next

技术图片
 

3.3、项目结构

技术图片

四、添加文件

 

添加静态文件

 

xml version="1.0" encoding="UTF-8"?>
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    modelVersion>4.0.0modelVersion>
    parent>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-parentartifactId>
        version>2.2.6.RELEASEversion>
        relativePath/> 
    parent>
    groupId>org.ouyushngroupId>
    artifactId>spring-boot-error-pageartifactId>
    version>0.0.1-SNAPSHOTversion>
    name>spring-boot-error-pagename>
    description>Error page project for Spring Bootdescription>

    properties>
        java.version>1.8java.version>
    properties>

    dependencies>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-freemarkerartifactId>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>

        dependency>
            groupId>org.webjarsgroupId>
            artifactId>jqueryartifactId>
            version>3.5.0version>
        dependency>

        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-testartifactId>
            scope>testscope>
            exclusions>
                exclusion>
                    groupId>org.junit.vintagegroupId>
                    artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    build>
        plugins>
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

 

添加index.ftl

 

DOCTYPE html>
html>
head lang="en">
    title>Spring Boot Demo - FreeMarkertitle>

    link href="/css/index.css" rel="stylesheet" />

head>
body>
div style="text-align: center;">
    img src="/images/springboot.jpg" />
    h1 id="title">${title}h1>
div>

script type="text/javascript" src="/webjars/jquery/3.3.1/jquery.min.js">script>

script>
    $(function(){
        $(#title).click(function(){
            alert(点击了);
        });
    })
script>
body>
html>

 

添加index.css
h1{color: blue;}

 

配置文件application.properties

spring.freemarker.template-loader-path=classpath:/templates
spring.freemarker.suffix=.ftl

404.html
DOCTYPE html>
html>
head lang="en">
    meta charset="UTF-8">
    title>Spring Boot Demo - FreeMarkertitle>
head>
body>
h1>404-找不到页面-publich1>
body>
html>

五、测试

http://localhost:8080/user/index/
技术图片
 
http://localhost:8080/user/a/

 

404-找不到页面-public
 
http://localhost:8080/user/error/

 

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu May 07 13:42:39 CST 2020
There was an unexpected error (type=Internal Server Error, status=500).
测试异常
 

Spring boot Sample 014之spring-boot-error-page

标签:user   strong   net   静态   char   测试   unit   prope   mave   

原文地址:https://www.cnblogs.com/ouyushan/p/13030242.html


评论


亲,登录后才可以留言!