
com.jfinal.render.ErrorRender Maven / Gradle / Ivy
/**
* Copyright (c) 2011-2017, James Zhan 詹波 ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jfinal.render;
import java.io.IOException;
import java.io.PrintWriter;
import com.jfinal.core.Const;
/**
* ErrorRender.
*/
public class ErrorRender extends Render {
protected static final String contentType = "text/html; charset=" + getEncoding();
protected static final String version = "Powered by JFinal " + Const.JFINAL_VERSION + " ";
protected static final String html404 = "404 Not Found 404 Not Found
" + version + "";
protected static final String html500 = "500 Internal Server Error 500 Internal Server Error
" + version + "";
protected static final String html401 = "401 Unauthorized 401 Unauthorized
" + version + "";
protected static final String html403 = "403 Forbidden 403 Forbidden
" + version + "";
protected int errorCode;
public ErrorRender(int errorCode, String view) {
this.errorCode = errorCode;
this.view = view;
}
public void render() {
response.setStatus(getErrorCode()); // HttpServletResponse.SC_XXX_XXX
// render with view
String view = getView();
if (view != null) {
RenderManager.me().getRenderFactory().getRender(view).setContext(request, response).render();
return;
}
// render with html content
PrintWriter writer = null;
try {
response.setContentType(contentType);
writer = response.getWriter();
writer.write(getErrorHtml());
writer.flush();
} catch (IOException e) {
throw new RenderException(e);
}
}
public String getErrorHtml() {
int errorCode = getErrorCode();
if (errorCode == 404)
return html404;
if (errorCode == 500)
return html500;
if (errorCode == 401)
return html401;
if (errorCode == 403)
return html403;
return "" + errorCode + " Error " + errorCode + " Error
" + version + "";
}
public int getErrorCode() {
return errorCode;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy