org.springframework.web.servlet.ModelAndView.setStatus()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(3.7k)|赞(0)|评价(0)|浏览(324)

本文整理了Java中org.springframework.web.servlet.ModelAndView.setStatus()方法的一些代码示例,展示了ModelAndView.setStatus()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ModelAndView.setStatus()方法的具体详情如下:
包路径:org.springframework.web.servlet.ModelAndView
类名称:ModelAndView
方法名:setStatus

ModelAndView.setStatus介绍

[英]Set the HTTP status to use for the response.

The response status is set just prior to View rendering.
[中]设置用于响应的HTTP状态。
响应状态是在视图渲染之前设置的。

代码示例

代码示例来源:origin: com.github.vindell/spring-biz

public static ModelAndView textView(String content, HttpStatus status) {
  // 使用Static视图
  TextResourceView view = new TextResourceView(content);
  // 构造ModelAndView
  ModelAndView mav = new ModelAndView(view);
  mav.setStatus(status);
  return mav;
}

代码示例来源:origin: com.github.vindell/spring-biz

public static ModelAndView htmlView(String content, HttpStatus status) {
  // 使用Html视图
  HtmlResourceView view = new HtmlResourceView(content);
  // 构造ModelAndView
  ModelAndView mav = new ModelAndView(view);
  mav.setStatus(status);
  return mav;
}

代码示例来源:origin: com.github.vindell/spring-biz

public static ModelAndView jsonView(String content, HttpStatus status) {
  // 使用json视图
  JSONResourceView view = new JSONResourceView(content);
  // 构造ModelAndView
  ModelAndView mav = new ModelAndView(view);
  mav.setStatus(status);
  
  return mav;
}

代码示例来源:origin: com.github.vindell/spring-biz

public static ModelAndView xmlView(String content, HttpStatus status ) {
  // 使用xml视图
  XMLResourceView xmlView = new XMLResourceView(content);
  
  // 构造ModelAndView
  ModelAndView mav = new ModelAndView(xmlView);
  mav.setStatus(status);
  return mav;
}

代码示例来源:origin: gexiangdong/tutorial

/**
 * 返回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;
}

代码示例来源:origin: com.github.vindell/spring-biz

public static ModelAndView staticView(String content, String contentType) {
  // 使用Static视图
  StaticResourceView staticView = new StaticResourceView(content);
  // 设置响应头
  staticView.setContentType(contentType);
  // 构造ModelAndView
  ModelAndView mav = new ModelAndView(staticView);
  mav.setStatus(HttpStatus.OK);
  return mav;
}

代码示例来源:origin: com.github.vindell/spring-biz

public static ModelAndView staticView(String content, HttpStatus status, String contentType) {
  // 使用Static视图
  StaticResourceView staticView = new StaticResourceView(content);
  // 设置响应头
  staticView.setContentType(contentType);
  // 构造ModelAndView
  ModelAndView mav = new ModelAndView(staticView);
  mav.setStatus(status);
  return mav;
}

代码示例来源:origin: org.apereo.cas/cas-server-support-oauth-core

/**
 * Write to the output this error.
 *
 * @param response the response
 * @param error    error message
 * @return json -backed view.
 */
public static ModelAndView writeError(final HttpServletResponse response, final String error) {
  val model = CollectionUtils.wrap(OAuth20Constants.ERROR, error);
  val mv = new ModelAndView(new MappingJackson2JsonView(MAPPER), (Map) model);
  mv.setStatus(HttpStatus.BAD_REQUEST);
  response.setStatus(HttpStatus.BAD_REQUEST.value());
  return mv;
}

代码示例来源:origin: code4everything/efo

@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;
  }
}

相关文章