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

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

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

ModelAndView.getModel介绍

[英]Return the model map. Never returns null. To be called by application code for modifying the model.
[中]返回模型地图。从不返回null。由应用程序代码调用以修改模型。

代码示例

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

/**
 * Assert whether or not a model attribute is available.
 * @param mav the ModelAndView to test against (never {@code null})
 * @param modelName name of the object to add to the model (never {@code null})
 */
public static void assertModelAttributeAvailable(ModelAndView mav, String modelName) {
  Map<String, Object> model = mav.getModel();
  assertTrue("Model attribute with name '" + modelName + "' is not available", model.containsKey(modelName));
}

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

private BindingResult getBindingResult(ModelAndView mav, String name) {
  BindingResult result = (BindingResult) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + name);
  if (result == null) {
    fail("No BindingResult for attribute: " + name);
  }
  return result;
}

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

/**
 * Assert a model attribute value.
 */
public ResultMatcher attribute(final String name, final Object value) {
  return result -> {
    ModelAndView mav = getModelAndView(result);
    assertEquals("Model attribute '" + name + "'", value, mav.getModel().get(name));
  };
}

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

/**
 * Assert the given model attributes do not exist.
 */
public ResultMatcher attributeDoesNotExist(final String... names) {
  return result -> {
    ModelAndView mav = getModelAndView(result);
    for (String name : names) {
      assertTrue("Model attribute '" + name + "' exists", mav.getModel().get(name) == null);
    }
  };
}

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

@Test
public void handleRequestWithFlashAttributes() throws Exception {
  this.request.setAttribute(DispatcherServlet.INPUT_FLASH_MAP_ATTRIBUTE, new ModelMap("name", "value"));
  ModelAndView mav = this.controller.handleRequest(this.request, new MockHttpServletResponse());
  assertEquals(1, mav.getModel().size());
  assertEquals("value", mav.getModel().get("name"));
}

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

@Test
public void modelAttributePackageNameAdvice() throws Exception {
  this.webAppContext.registerSingleton("mapa", ModelAttributePackageAdvice.class);
  this.webAppContext.registerSingleton("manupa", ModelAttributeNotUsedPackageAdvice.class);
  this.webAppContext.refresh();
  HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
  this.handlerAdapter.afterPropertiesSet();
  ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod);
  assertEquals("lAttr1", mav.getModel().get("attr1"));
  assertEquals("gAttr2", mav.getModel().get("attr2"));
  assertEquals(null,mav.getModel().get("attr3"));
}

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

@Test
public void modelAttributeAdvice() throws Exception {
  this.webAppContext.registerSingleton("maa", ModelAttributeAdvice.class);
  this.webAppContext.refresh();
  HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
  this.handlerAdapter.afterPropertiesSet();
  ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod);
  assertEquals("lAttr1", mav.getModel().get("attr1"));
  assertEquals("gAttr2", mav.getModel().get("attr2"));
}

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

@Test
public void withFlashAttributes() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index");
  request.setAttribute(DispatcherServlet.INPUT_FLASH_MAP_ATTRIBUTE, new ModelMap("name", "value"));
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("index", mv.getViewName());
  assertEquals(1, mv.getModel().size());
  assertEquals("value", mv.getModel().get("name"));
}

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

@Test
public void nullExceptionAttribute() {
  exceptionResolver.setDefaultErrorView("default-view");
  exceptionResolver.setExceptionAttribute(null);
  ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException);
  assertEquals("default-view", mav.getViewName());
  assertNull(mav.getModel().get(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
}

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

@Test
public void handleRequestWithViewName() throws Exception {
  String viewName = "testView";
  this.controller.setViewName(viewName);
  ModelAndView mav = this.controller.handleRequest(this.request, new MockHttpServletResponse());
  assertEquals(viewName, mav.getViewName());
  assertTrue(mav.getModel().isEmpty());
}

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

@Test
public void withFilenameAndMatrixVariables() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index;a=A;b=B");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("index", mv.getViewName());
  assertTrue(mv.getModel().isEmpty());
}

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

@Test
public void withFilenamePlusExtension() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("index", mv.getViewName());
  assertTrue(mv.getModel().isEmpty());
}

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

@Test
public void multiLevel() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/docs/cvs/commit.html");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("docs/cvs/commit", mv.getViewName());
  assertTrue(mv.getModel().isEmpty());
}

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

@Test
public void withPlainFilename() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("index", mv.getViewName());
  assertTrue(mv.getModel().isEmpty());
}

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

@Test
public void withPrefix() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  ctrl.setPrefix("mypre_");
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("mypre_index", mv.getViewName());
  assertTrue(mv.getModel().isEmpty());
}

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

@Test
public void withSuffix() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  ctrl.setSuffix("_mysuf");
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("index_mysuf", mv.getViewName());
  assertTrue(mv.getModel().isEmpty());
}

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

@Test
public void multiLevelWithMapping() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/docs/cvs/commit.html");
  exposePathInMapping(request, "/docs/**");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("cvs/commit", mv.getViewName());
  assertTrue(mv.getModel().isEmpty());
}

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

@Test
public void withContextMapping() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/docs/cvs/commit.html");
  request.setContextPath("/myapp");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("docs/cvs/commit", mv.getViewName());
  assertTrue(mv.getModel().isEmpty());
}

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

@Test
public void multiLevelMappingWithFallback() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/docs/cvs/commit.html");
  exposePathInMapping(request, "/docs/cvs/commit.html");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("docs/cvs/commit", mv.getViewName());
  assertTrue(mv.getModel().isEmpty());
}

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

@Test
public void withPrefixAndSuffix() throws Exception {
  UrlFilenameViewController ctrl = new UrlFilenameViewController();
  ctrl.setPrefix("mypre_");
  ctrl.setSuffix("_mysuf");
  MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ModelAndView mv = ctrl.handleRequest(request, response);
  assertEquals("mypre_index_mysuf", mv.getViewName());
  assertTrue(mv.getModel().isEmpty());
}

相关文章