From 803b4fcbd12515cd189ccb0e60746db8e762b6d6 Mon Sep 17 00:00:00 2001 From: "1719411461@qq.com" Date: Tue, 8 May 2018 19:28:48 +0800 Subject: [PATCH 1/4] =?UTF-8?q?add:=E5=85=81=E8=AE=B8=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89ErrorHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nutz/demo/simple/handler/MyHandler.java | 24 +++++++++++++++++++ .../nutz/boot/starter/jetty/JettyStarter.java | 8 ++++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java diff --git a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java new file mode 100644 index 00000000..77a7647b --- /dev/null +++ b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java @@ -0,0 +1,24 @@ +package io.nutz.demo.simple.handler; + +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.servlet.ErrorPageErrorHandler; +import org.nutz.ioc.loader.annotation.IocBean; +import org.nutz.json.Json; +import org.nutz.lang.util.NutMap; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +@IocBean +public class MyHandler extends ErrorPageErrorHandler { + @Override + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + if(response.getStatus() == 404){ + response.getWriter().write(Json.toJson(new NutMap("code","-1").setv("msg","404 error"))); + return; + } + super.handle(target, baseRequest, request, response); + } +} diff --git a/nutzboot-starter/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java b/nutzboot-starter/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java index 29b3d084..d87fd798 100644 --- a/nutzboot-starter/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java +++ b/nutzboot-starter/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java @@ -194,7 +194,13 @@ public class JettyStarter extends AbstractServletContainerStarter implements Ser wac.getServletContext().setExtendedListenerTypes(true); wac.getSessionHandler().setMaxInactiveInterval(getSessionTimeout()); - ErrorPageErrorHandler ep = new ErrorPageErrorHandler(); + List errorHandlers = appContext.getBeans(ErrorPageErrorHandler.class); + ErrorPageErrorHandler ep; + if(errorHandlers.size() > 0){ + ep = errorHandlers.get(0); + }else{ + ep = new ErrorPageErrorHandler(); + } ep.setErrorPages(getErrorPages()); wac.setErrorHandler(ep); From 5b060c3636ff54a716ca0a8d0586197b22d2765e Mon Sep 17 00:00:00 2001 From: "1719411461@qq.com" Date: Tue, 8 May 2018 19:47:04 +0800 Subject: [PATCH 2/4] =?UTF-8?q?change:=20=E4=BF=AE=E6=94=B9=E4=B8=80?= =?UTF-8?q?=E4=B8=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/nutz/boot/starter/jetty/JettyStarter.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/nutzboot-starter/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java b/nutzboot-starter/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java index d87fd798..66c3efa7 100644 --- a/nutzboot-starter/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java +++ b/nutzboot-starter/nutzboot-starter-jetty/src/main/java/org/nutz/boot/starter/jetty/JettyStarter.java @@ -18,6 +18,7 @@ import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.servlet.ErrorPageErrorHandler; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceCollection; @@ -194,14 +195,12 @@ public class JettyStarter extends AbstractServletContainerStarter implements Ser wac.getServletContext().setExtendedListenerTypes(true); wac.getSessionHandler().setMaxInactiveInterval(getSessionTimeout()); - List errorHandlers = appContext.getBeans(ErrorPageErrorHandler.class); - ErrorPageErrorHandler ep; - if(errorHandlers.size() > 0){ - ep = errorHandlers.get(0); - }else{ - ep = new ErrorPageErrorHandler(); + ErrorHandler ep = Lang.first(appContext.getBeans(ErrorHandler.class)); + if(ep == null){ + ErrorPageErrorHandler handler = new ErrorPageErrorHandler(); + handler.setErrorPages(getErrorPages()); + ep = handler; } - ep.setErrorPages(getErrorPages()); wac.setErrorHandler(ep); // 设置一下额外的东西 From 975c1916ceee4f1d13785f91f1cbef870216432b Mon Sep 17 00:00:00 2001 From: "1719411461@qq.com" Date: Tue, 8 May 2018 19:52:45 +0800 Subject: [PATCH 3/4] =?UTF-8?q?change:=20=E8=BF=99=E9=87=8C=E5=BF=98?= =?UTF-8?q?=E6=94=B9=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/io/nutz/demo/simple/handler/MyHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java index 77a7647b..4f1cb929 100644 --- a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java +++ b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java @@ -1,6 +1,7 @@ package io.nutz.demo.simple.handler; import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.servlet.ErrorPageErrorHandler; import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.json.Json; @@ -12,7 +13,7 @@ import java.io.IOException; import java.io.PrintWriter; @IocBean -public class MyHandler extends ErrorPageErrorHandler { +public class MyHandler extends ErrorHandler { @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { if(response.getStatus() == 404){ From e4b624b50fd11cf54ad99082abad8b10433677eb Mon Sep 17 00:00:00 2001 From: "1719411461@qq.com" Date: Tue, 8 May 2018 20:02:47 +0800 Subject: [PATCH 4/4] =?UTF-8?q?change:=20=E5=86=8D=E6=94=B9=E4=B8=80?= =?UTF-8?q?=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/io/nutz/demo/simple/handler/MyHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java index 4f1cb929..785a7a6f 100644 --- a/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java +++ b/nutzboot-demo/nutzboot-demo-simple/nutzboot-demo-simple-jetty/src/main/java/io/nutz/demo/simple/handler/MyHandler.java @@ -13,7 +13,7 @@ import java.io.IOException; import java.io.PrintWriter; @IocBean -public class MyHandler extends ErrorHandler { +public class MyHandler extends ErrorPageErrorHandler { @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { if(response.getStatus() == 404){