Java Code Examples for org.springframework.web.servlet.ModelAndView#setStatus()

The following examples show how to use org.springframework.web.servlet.ModelAndView#setStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GlobalExceptionHandler.java    From mPaaS with Apache License 2.0 6 votes vote down vote up
@Override
public ModelAndView resolveException(HttpServletRequest request,
                                     HttpServletResponse response,
                                     Object handler,
                                     Exception ex) {
    String traceid = TraceUtil.getTraceId();
    Determine determine = ExceptionUtil.determineType(ex);
    log.error("TraceId[{}]发生异常[{}]", traceid, determine.getName(), ex);
    ModelAndView mv = new ModelAndView(view);
    Response<?> result = null;
    LinkedList<Stack> stacks = ExceptionUtil.getStacks(ex, app, traceid);
    Stack stack = stacks.getFirst();
    Map<String, Object> data = new HashMap<>();
    data.put("traceid", traceid);
    data.put("stacks", stacks);
    result = Response.err(stack.getCode(), stack.getMessage(), data);
    mv.setStatus(determine.getStatus());
    return mv.addObject(result);
}
 
Example 2
Source File: MultiModelAndViewReturnValueHandler.java    From xiaoyaoji with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if(!(returnValue instanceof MultiView)){
        processor.handleReturnValue(new Result<>(true,returnValue),returnType,mavContainer,webRequest);
        return;
    }
    MultiView view = (MultiView) returnValue;
    //如果是ajax请求,则返回json
    if("XMLHttpRequest".equals(webRequest.getHeader("X-Requested-With"))){
        processor.handleReturnValue(new Result<>(true,view.getModelMap()),returnType,mavContainer,webRequest);
        return;
    }

    ModelAndView temp = new ModelAndView(view.getViewName());
    if(view.getView() != null) {
        temp.setView(view.getView());
    }
    temp.setStatus(view.getStatus());
    temp.addAllObjects(view.getModelMap());
    super.handleReturnValue(temp, returnType, mavContainer, webRequest);
}
 
Example 3
Source File: GlobalExceptionHandler.java    From efo with MIT License 5 votes vote down vote up
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
                                     Exception ex) {
    ModelAndView mv = new ModelAndView();
    FastJsonJsonView view = new FastJsonJsonView();
    Map<String, Object> attributes = new HashMap<>(ValueConsts.TWO_INT);
    attributes.put("code", "502");
    attributes.put("message", ex.getMessage());
    String queryString = request.getQueryString();
    attributes.put("url", request.getRequestURI() + (Checker.isEmpty(queryString) ? "" : "?" + queryString));
    view.setAttributesMap(attributes);
    mv.setView(view);
    mv.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
    return mv;
}
 
Example 4
Source File: MvcController.java    From tutorial with MIT License 5 votes vote down vote up
/**
 * 返回ModelAndView,可以在返回的ModelAndView里设置Model,也设置View(使用的模版)
 * @return
 */
@GetMapping("/hello")
public ModelAndView hello() {
    //model.addAttribute("name", name);
    log.trace("MvcController.hello();...");
    ModelAndView mv = new ModelAndView(); //"hi";
    mv.setViewName("hi");
    mv.setStatus(HttpStatus.OK);
    mv.addObject("name", "modelandview");
    return mv;
}
 
Example 5
Source File: ViewExceptionHandler.java    From jakduk-api with MIT License 5 votes vote down vote up
@ExceptionHandler({ ServiceException.class })
public ModelAndView handleServiceException(HttpServletRequest request, ServiceException exception) {

    ServiceError serviceError = exception.getServiceError();
    HttpStatus httpStatus = HttpStatus.valueOf(serviceError.getHttpStatus());

    RestErrorResponse restErrorResponse = new RestErrorResponse(serviceError, exception.getLocalizedMessage());

    String logMessage;

    try {
        logMessage = ObjectMapperUtils.writeValueAsString(restErrorResponse);
    } catch (JsonProcessingException e) {
        logMessage = "code : " + exception.getServiceError().getCode() + ", message : " + exception.getLocalizedMessage();
    }

    ModelAndView modelAndView = new ModelAndView();

    if (httpStatus.is4xxClientError()) {
        modelAndView.setViewName("error/4xx");
        log.warn(logMessage, exception);

    } else if (httpStatus.is5xxServerError()) {
        modelAndView.setViewName("error/" + httpStatus.toString());
        log.error(logMessage, exception);
    }

    Map<String, Object> map = getErrorAttributes(request, false);
    modelAndView.addAllObjects(map);
    modelAndView.setStatus(httpStatus);
    modelAndView.addObject("status", httpStatus.value());

    return modelAndView;
}
 
Example 6
Source File: ErrorTicketService.java    From codenjoy with GNU General Public License v3.0 4 votes vote down vote up
public ModelAndView get(String url, Exception exception) {
    String ticket = ticket();

    String message = printStackTrace ? exception.toString() : exception.toString();
    log.error("[TICKET:URL] {}:{} {}", ticket, url, message);
    System.err.printf("[TICKET:URL] %s:%s %s%n", ticket, url, message);

    if (printStackTrace && !skip(message)) {
        exception.printStackTrace();
    }

    Map<String, Object> info = getDetails(ticket, url, exception);
    tickets.put(ticket, info);

    ModelAndView result = new ModelAndView();
    result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);

    copy("ticketNumber", info, result);

    if (!debug.isWorking()) {
        result.addObject("message", getMessage());

        if (url.contains("/rest/")) {
            shouldJsonResult(result);
        } else {
            shouldErrorPage(result);
        }
        return result;
    }

    copy("message", info, result);
    copy("url", info, result);
    copy("exception", info, result);

    if (url.contains("/rest/")) {
        copy("stackTrace", info, result);

        result.setView(new MappingJackson2JsonView(){{
            setPrettyPrint(true);
        }});

        return result;
    }

    result.addObject("stackTrace", prepareStackTrace(exception));

    shouldErrorPage(result);
    return result;
}
 
Example 7
Source File: ErrorTicketService.java    From codenjoy with GNU General Public License v3.0 4 votes vote down vote up
public ModelAndView get(String url, Exception exception) {
    String ticket = ticket();

    String message = printStackTrace ? exception.toString() : exception.toString();
    log.error("[TICKET:URL] {}:{} {}", ticket, url, message);
    System.err.printf("[TICKET:URL] %s:%s %s%n", ticket, url, message);

    if (printStackTrace && !skip(message)) {
        exception.printStackTrace();
    }

    Map<String, Object> info = getDetails(ticket, url, exception);
    tickets.put(ticket, info);

    ModelAndView result = new ModelAndView();
    result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);

    copy("ticketNumber", info, result);

    if (!debug.isWorking()) {
        result.addObject("message", ERROR_MESSAGE);

        if (url.contains("/rest/")) {
            shouldJsonResult(result);
        } else {
            shouldErrorPage(result);
        }
        return result;
    }


    copy("message", info, result);
    copy("url", info, result);
    copy("exception", info, result);

    if (url.contains("/rest/")) {
        copy("stackTrace", info, result);

        result.setView(new MappingJackson2JsonView(){{
            setPrettyPrint(true);
        }});
        return result;
    }

    result.addObject("stackTrace", prepareStackTrace(exception));

    shouldErrorPage(result);
    return result;
}