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

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

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

ModelAndView.addObject介绍

[英]Add an attribute to the model using parameter name generation.
[中]使用参数名生成向模型添加属性。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return a ModelAndView for the given view name and exception.
 * <p>The default implementation adds the specified exception attribute.
 * Can be overridden in subclasses.
 * @param viewName the name of the error view
 * @param ex the exception that got thrown during handler execution
 * @return the ModelAndView instance
 * @see #setExceptionAttribute
 */
protected ModelAndView getModelAndView(String viewName, Exception ex) {
  ModelAndView mv = new ModelAndView(viewName);
  if (this.exceptionAttribute != null) {
    mv.addObject(this.exceptionAttribute, ex);
  }
  return mv;
}

代码示例来源:origin: spring-projects/spring-framework

@RequestMapping
@JsonView(MyJacksonView2.class)
public ResponseEntity<JacksonViewBean> handleResponseEntity() {
  JacksonViewBean bean = new JacksonViewBean();
  bean.setWithView1("with");
  bean.setWithView2("with");
  bean.setWithoutView("without");
  ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
  mav.addObject("bean", bean);
  return new ResponseEntity<>(bean, HttpStatus.OK);
}

代码示例来源:origin: bill1012/AdminEAP

@RequestMapping(value = "/redis/connect",method = RequestMethod.GET)
private ModelAndView redisConnectFailure(ModelAndView modelAndView, HttpServletRequest request){
  String basePath = request.getContextPath();
  request.setAttribute("basePath", basePath);
  modelAndView.addObject("errorName","redisConnectionFailure");
  modelAndView.addObject("message","连接redis错误,请确认redis配置正确,且设置了正确的密码");
  modelAndView.addObject("detail", ErrorConstant.ERROR_DETAIL);
  modelAndView.setViewName("base/error/error");
  return modelAndView;
}

代码示例来源:origin: timebusker/spring-boot

@RequestMapping("/free1")
public ModelAndView free1() {
  ModelAndView mv1 = new ModelAndView();
  mv1.addObject("intVar", 100);
  mv1.addObject("LongVar", 10000000000000000L);
  mv1.addObject("doubleVar", 3.141592675d);
  mv1.addObject("StringVar", "我是freemarker,是字符串!");
  mv1.addObject("booleanVar", true);
  mv1.addObject("dateVar1", new Date());
  mv1.addObject("nullVar1", null);
  mv1.addObject("nullVar", "我是空");
  return mv1;
}

代码示例来源:origin: stackoverflow.com

@ControllerAdvice
public class ExceptionHandlerController {

  public static final String DEFAULT_ERROR_VIEW = "error";

  @ExceptionHandler(value = {Exception.class, RuntimeException.class})
  public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) {
      ModelAndView mav = new ModelAndView(DEFAULT_ERROR_VIEW);

    mav.addObject("datetime", new Date());
    mav.addObject("exception", e);
    mav.addObject("url", request.getRequestURL());
    return mav;
  }
}

代码示例来源:origin: timebusker/spring-boot

@RequestMapping(value = "/free0")
public ModelAndView index() {
  ModelAndView mv = new ModelAndView();
  mv.addObject("username", "你好!Freemarker!");
  return mv;
}

代码示例来源:origin: stackoverflow.com

@ControllerAdvice
public class MyExceptionController {
  @ExceptionHandler(NoHandlerFoundException.class)
  public ModelAndView handleError404(HttpServletRequest request, Exception e)   {
      ModelAndView mav = new ModelAndView("/404");
      mav.addObject("exception", e);  
      //mav.addObject("errorcode", "404");
      return mav;
  }
}

代码示例来源:origin: timebusker/spring-boot

@RequestMapping(value = "/free3")
public ModelAndView free3() {
  ModelAndView mv3 = new ModelAndView();
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("Java", "你好Java");
  map.put("address", "北京");
  map.put("身高", 172);
  map.put("money", 100.5);
  mv3.addObject("map", map);
  return mv3;
}

代码示例来源:origin: org.springframework/spring-webmvc

/**
 * Return a ModelAndView for the given view name and exception.
 * <p>The default implementation adds the specified exception attribute.
 * Can be overridden in subclasses.
 * @param viewName the name of the error view
 * @param ex the exception that got thrown during handler execution
 * @return the ModelAndView instance
 * @see #setExceptionAttribute
 */
protected ModelAndView getModelAndView(String viewName, Exception ex) {
  ModelAndView mv = new ModelAndView(viewName);
  if (this.exceptionAttribute != null) {
    mv.addObject(this.exceptionAttribute, ex);
  }
  return mv;
}

代码示例来源:origin: timebusker/spring-boot

@RequestMapping("/free2")
public ModelAndView free2() {
  ModelAndView mv2 = new ModelAndView();
  List<String> list = new ArrayList<String>();
  list.add("java");
  list.add("JavaScript");
  list.add("python");
  list.add("php");
  list.add("Html");
  mv2.addObject("programList", list);
  return mv2;
}

代码示例来源:origin: stackoverflow.com

ModelAndView modelAndView =  new ModelAndView("redirect:/abc.htm");
modelAndView.addObject("modelAttribute" , new ModelAttribute());
return modelAndView;

代码示例来源:origin: timebusker/spring-boot

@RequestMapping("/free4")
public ModelAndView free4() {
  ModelAndView mv4 = new ModelAndView();
  mv4.addObject("sort_int", new SortMethod());
  return mv4;
}

代码示例来源:origin: Raysmond/SpringBlog

@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
public ModelAndView notFound(HttpServletRequest request, NotFoundException exception) {
  String uri = request.getRequestURI();
  log.error("Request page: {} raised NotFoundException {}", uri, exception);
  ModelAndView model = new ModelAndView("error/general");
  model.addObject("status", HttpStatus.NOT_FOUND.value());
  model.addObject("error", HttpStatus.NOT_FOUND.getReasonPhrase());
  model.addObject("path", uri);
  model.addObject("customMessage", exception.getMessage());
  return model;
}

代码示例来源:origin: timebusker/spring-boot

@RequestMapping("/entity")
  public ModelAndView index() {
    ModelAndView mv = new ModelAndView();
    int sum = 12 + 13;
    UserInfo u = service.getUser();
    Map<String, UserInfo> hu = service.allUser();
    Collection<UserInfo> cu = service.allUser().values();
    List<UserInfo> lu = new ArrayList<>(cu);
    mv.addObject("sum", sum);
    mv.addObject("u", u);
    mv.addObject("hu", hu);
    mv.addObject("lu", lu);
    return mv;
  }
}

代码示例来源:origin: Raysmond/SpringBlog

/**
   * Handle all exceptions
   */
//    @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
  @ExceptionHandler(Exception.class)
  public ModelAndView exception(HttpServletRequest request, Exception exception) {
    String uri = request.getRequestURI();
    log.error("Request page: {} raised exception {}", uri, exception);

    ModelAndView model = new ModelAndView("error/general");
    model.addObject("error", Throwables.getRootCause(exception).getMessage());
    model.addObject("status", Throwables.getRootCause(exception).getCause());
    model.addObject("path", uri);
    model.addObject("customMessage", exception.getMessage());

    return model;
  }
}

代码示例来源:origin: ucarGroup/DataLink

@RequestMapping(value = "/toRestartMysqlTask")
public ModelAndView toRestartMysqlTask(Long id) {
  ModelAndView mav = new ModelAndView("task/mysqlTaskRestart");
  mav.addObject("id", id);
  return mav;
}

代码示例来源:origin: spring-projects/spring-petclinic

/**
 * Custom handler for displaying an owner.
 *
 * @param ownerId the ID of the owner to display
 * @return a ModelMap with the model attributes for the view
 */
@GetMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
  ModelAndView mav = new ModelAndView("owners/ownerDetails");
  mav.addObject(this.owners.findById(ownerId));
  return mav;
}

代码示例来源:origin: pl.edu.icm.synat/synat-console-core

@RequestMapping(value = "/licensing/organisation_group/news/add/{organisationGroupId}", method = RequestMethod.GET)
public ModelAndView addOrganisationGroupNewsForm(@PathVariable Long organisationGroupId, @RequestParam("redirect") String redirect, @ModelAttribute("news") News news, BindingResult bindingResult) {
  ModelAndView results = new ModelAndView("container.platform.licensing.news.add");
  results.addObject("organisationGroupId", organisationGroupId);
  results.addObject("redirect", redirect);
  return results;
}

代码示例来源:origin: roncoo/spring-boot-demo

/**
 * 统一异常处理
 * 
 * @param exception
 *            exception
 * @return
 */
@ExceptionHandler({ RuntimeException.class })
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(RuntimeException exception) {
  logger.info("自定义异常处理-RuntimeException");
  ModelAndView m = new ModelAndView();
  m.addObject("roncooException", exception.getMessage());
  m.setViewName("error/500");
  return m;
}

代码示例来源:origin: ucarGroup/DataLink

@RequestMapping(value = "/toTaskException")
public ModelAndView toTaskException(HttpServletRequest request) {
  Long taskId = Long.valueOf(request.getParameter("taskId"));
  ModelAndView mav = new ModelAndView("taskMonitor/taskException");
  mav.addObject("taskId", taskId);
  return mav;
}

相关文章