Play2.6 Java开发模式禁用调试视图,配置自定义错误页
Got it, let's fix this! Play 2.6's dev mode defaults to showing verbose debug pages, but we can override that with a custom ErrorHandler that works seamlessly in both dev and production environments. Here's a step-by-step solution tailored to your needs:
1. Create a Custom Error Handler Class
First, we'll extend Play's DefaultHttpErrorHandler to override how errors are handled. This class will redirect 404s to your 404err.scala.html, general server errors to 500err.scala.html, and database connection errors to dbDownErr.scala.html.
Create a new Java class (e.g., CustomErrorHandler.java) in your application's package (replace com.yourapp with your actual package name):
import play.http.DefaultHttpErrorHandler; import play.mvc.Http; import play.mvc.Result; import play.mvc.Results; import javax.inject.Inject; import javax.inject.Singleton; import java.sql.SQLException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; @Singleton public class CustomErrorHandler extends DefaultHttpErrorHandler { @Inject public CustomErrorHandler(HttpErrorHandler.Context context) { super(context); } // Handle client-side errors like 404 @Override protected CompletionStage<Result> onClientError(Http.RequestHeader request, int statusCode, String message) { if (statusCode == 404) { return CompletableFuture.completedFuture( Results.notFound(views.html.404err.render()) ); } // Fallback to default for other client errors if needed return super.onClientError(request, statusCode, message); } // Handle server-side errors including database issues @Override protected CompletionStage<Result> onServerError(Http.RequestHeader request, Throwable exception) { // Detect database connection errors (adjust the check based on your actual exception type) if (exception instanceof SQLException || (exception.getCause() != null && exception.getCause() instanceof SQLException)) { return CompletableFuture.completedFuture( Results.internalServerError(views.html.dbDownErr.render()) ); } // Catch-all for other 500 errors return CompletableFuture.completedFuture( Results.internalServerError(views.html.500err.render()) ); } }
2. Register the Custom Error Handler
Next, tell Play to use your custom handler instead of the default one. Open your conf/application.conf file and add this line:
play.http.errorHandler = "com.yourapp.CustomErrorHandler"
Make sure to replace com.yourapp with the actual package path where you saved your CustomErrorHandler class.
3. Verify Your Setup
- Ensure your custom error views (
404err.scala.html,500err.scala.html,dbDownErr.scala.html) are located in theviewsdirectory of your Play project. - Start (or restart) your application in dev mode. Now, when you trigger a 404 error, a general server error, or a database connection issue, you should see your custom pages instead of Play's debug views.
A quick note: Play's dev mode hot-reloading will pick up changes to your error handler or views automatically, so you don't need to restart the app every time you tweak things.
内容的提问来源于stack exchange,提问作者Kortas Aladinne

