如何自定义跳转页面

3 篇文章 0 订阅
3 篇文章 0 订阅

Tomcat子代的错误页面太难看了,开发中我们可能要重写一下。这里有两种方式可以实现。

第一种方式

在web.xml中配置error-page标签

发生错误的页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        int i = 1/0;
    %>
</body>
</html>

web.xml配置

  <error-page>
    <error-code>500</error-code>
    <location>/errors/500.jsp</location>
  </error-page>

自定义的错误页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="js/jquery-3.1.1.min.js"></script>
<style>
    * {
        margin: 0px;
        padding: 0px;
    }
    h1 {
        line-height: 300px;
        text-align: center;
    }
</style>
</head>
<body>

</body>
</html>
<script>
    $(function() {
        var obj1 = $("<div></div>");
        obj1.html("<h1>500错误</h1>");
        obj1.css({"width":"300px", "height":"300px", "background-color":"rgb(0,0,0)", "position":"fixed", "top":"0px", "left":"0px", "border-radius":"150px"});
        obj1.prop("id","divId1");
        $("body").append(obj1);

        // 获取可移动区域屏幕的大小
        var borWidth = $(window).width()-$("#divId1").innerWidth();
        var borHeight = $(window).height()-$("#divId1").innerHeight();
        // alert(borWidth);

        //元素移动的方向
        var xMove = 1;
        var yMove = 1;


        //颜色值
        var r1;
        var g1;
        var b1;

        //设定计时器
        setInterval(move, 10);

        function move() {
            var x1 = parseInt($("#divId1").css("left"));
            var y1 = parseInt($("#divId1").css("top"));

            // alert(x);

            //获得偏移量
            x1 = x1 + xMove*3;
            y1 = y1 + yMove*2;

            //获得颜色值
            r1 = x1%256;
            g1 = y1%256;
            b1 = Math.floor((x1+y1)/2)%256;

            //判断位置
            if ((x1>=borWidth&&xMove==1)||(x1<=0&&xMove==-1)) {
                xMove*=-1;
            }
            if ((y1>=borHeight&&yMove==1)||(y1<=0&&yMove==-1)) {
                yMove*=-1;
            }

            $("#divId1").css({"top":y1+"px", "left":x1+"px","background-color":"rgb("+r1+","+g1+","+b1+")"});
        }
    }); 
</script>

第二种方式

在JSP指令中加入errorPage属性

发生错误的页面

<%@ page language="java" contentType="text/html; charset=UTF-8" errorPage="/errors/500.jsp"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        int i = 1/0;
    %>
</body>
</html>

自定义的错误页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="js/jquery-3.1.1.min.js"></script>
<style>
    * {
        margin: 0px;
        padding: 0px;
    }
    h1 {
        line-height: 300px;
        text-align: center;
    }
</style>
</head>
<body>

</body>
</html>
<script>
    $(function() {
        var obj1 = $("<div></div>");
        obj1.html("<h1>500错误</h1>");
        obj1.css({"width":"300px", "height":"300px", "background-color":"rgb(0,0,0)", "position":"fixed", "top":"0px", "left":"0px", "border-radius":"150px"});
        obj1.prop("id","divId1");
        $("body").append(obj1);

        // 获取可移动区域屏幕的大小
        var borWidth = $(window).width()-$("#divId1").innerWidth();
        var borHeight = $(window).height()-$("#divId1").innerHeight();
        // alert(borWidth);

        //元素移动的方向
        var xMove = 1;
        var yMove = 1;


        //颜色值
        var r1;
        var g1;
        var b1;

        //设定计时器
        setInterval(move, 10);

        function move() {
            var x1 = parseInt($("#divId1").css("left"));
            var y1 = parseInt($("#divId1").css("top"));

            // alert(x);

            //获得偏移量
            x1 = x1 + xMove*3;
            y1 = y1 + yMove*2;

            //获得颜色值
            r1 = x1%256;
            g1 = y1%256;
            b1 = Math.floor((x1+y1)/2)%256;

            //判断位置
            if ((x1>=borWidth&&xMove==1)||(x1<=0&&xMove==-1)) {
                xMove*=-1;
            }
            if ((y1>=borHeight&&yMove==1)||(y1<=0&&yMove==-1)) {
                yMove*=-1;
            }

            $("#divId1").css({"top":y1+"px", "left":x1+"px","background-color":"rgb("+r1+","+g1+","+b1+")"});
        }
    }); 
</script>

效果

给错误提示信息,加了一个弹球的效果。

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值