Java tutorial
/* * Copyright 2007-2016 the original author or authors. * * 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 net.ymate.platform.webmvc.view.impl; import net.ymate.platform.webmvc.context.WebContext; import net.ymate.platform.webmvc.view.AbstractView; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import javax.servlet.http.HttpServletResponse; /** * HTTP? * * @author (suninformation@163.com) on 2011-10-29 ?02:26:25 * @version 1.0 */ public class HttpStatusView extends AbstractView { /** * HTTP? */ private final int __status; private final String __msg; private final boolean __error; // private String __body; /** * STATUS: 405 */ public static HttpStatusView METHOD_NOT_ALLOWED = new HttpStatusView(HttpServletResponse.SC_METHOD_NOT_ALLOWED); /** * STATUS: 404 */ public static HttpStatusView NOT_FOUND = new HttpStatusView(HttpServletResponse.SC_NOT_FOUND); public static HttpStatusView bind(int status) { return new HttpStatusView(status); } public static HttpStatusView bind(int status, String msg) { return new HttpStatusView(status, msg); } /** * * * @param status HTTP? */ public HttpStatusView(int status) { __status = status; __msg = null; __error = true; } /** * * * @param status HTTP? * @param useError ?sendError */ public HttpStatusView(int status, boolean useError) { __status = status; __error = useError; __msg = null; } /** * * * @param status HTTP? * @param msg ??? */ public HttpStatusView(int status, String msg) { __status = status; __msg = msg; __error = false; } /** * ??(:useError=false) * * @param bodyStr * @return ? */ public HttpStatusView writeBody(String bodyStr) { __body = bodyStr; return this; } protected void __doRenderView() throws Exception { HttpServletResponse _response = WebContext.getResponse(); if (StringUtils.isNotBlank(__body)) { IOUtils.write(__body, _response.getOutputStream(), _response.getCharacterEncoding()); } if (StringUtils.isNotBlank(__msg)) { _response.sendError(__status, __msg); } else { if (__error) { _response.sendError(__status); } else { _response.setStatus(__status); } } } }