Java tutorial
/* * Project Name: qcpj * File Name: AuthController.java * Class Name: AuthController * * Copyright 2014 Hengtian Software Inc * * Licensed under the Hengtiansoft * * http://www.hengtiansoft.com * * 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 org.frat.common.controller; import java.text.MessageFormat; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * ClassName:ErrorController . * Description:TODO * * @author SC */ @Controller public class ErrorController extends AbstractController { @RequestMapping("error/404") public String generalError(HttpServletRequest request, HttpServletResponse response, Model model) { // retrieve some useful information from the request Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception"); String exceptionMessage = getExceptionMessage(throwable, statusCode); String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri"); if (requestUri == null) { requestUri = "Unknown"; } String message = MessageFormat.format("{0} returned for {1} with message {2}", statusCode, requestUri, exceptionMessage); model.addAttribute("errorMessage", message); response.setContentType("text/html;charset=UTF-8"); return "error/404"; } private String getExceptionMessage(Throwable throwable, Integer statusCode) { if (throwable != null) { return throwable.getMessage(); } HttpStatus httpStatus = HttpStatus.valueOf(statusCode); return httpStatus.getReasonPhrase(); } }