Example usage for org.springframework.mock.web.test MockRequestDispatcher MockRequestDispatcher

List of usage examples for org.springframework.mock.web.test MockRequestDispatcher MockRequestDispatcher

Introduction

In this page you can find the example usage for org.springframework.mock.web.test MockRequestDispatcher MockRequestDispatcher.

Prototype

public MockRequestDispatcher(String resource) 

Source Link

Document

Create a new MockRequestDispatcher for the given resource.

Usage

From source file:org.springframework.mock.web.test.MockServletContext.java

/**
 * Create a new {@code MockServletContext} using the supplied resource base
 * path and resource loader.//from  www  .  jav  a 2  s  .  c om
 * <p>Registers a {@link MockRequestDispatcher} for the Servlet named
 * {@literal 'default'}.
 * @param resourceBasePath the root directory of the WAR (should not end with a slash)
 * @param resourceLoader the ResourceLoader to use (or null for the default)
 * @see #registerNamedDispatcher
 */
public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader) {
    this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader());
    this.resourceBasePath = resourceBasePath;

    // Use JVM temp dir as ServletContext temp dir.
    String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY);
    if (tempDir != null) {
        this.attributes.put(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File(tempDir));
    }

    registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName));
}

From source file:org.springframework.mock.web.test.MockServletContext.java

@Override
public RequestDispatcher getRequestDispatcher(String path) {
    Assert.isTrue(path.startsWith("/"),
            () -> "RequestDispatcher path [" + path + "] at ServletContext level must start with '/'");
    return new MockRequestDispatcher(path);
}

From source file:org.springframework.mock.web.test.MockServletContext.java

/**
 * Set the name of the <em>default</em> {@code Servlet}.
 * <p>Also {@link #unregisterNamedDispatcher unregisters} the current default
 * {@link RequestDispatcher} and {@link #registerNamedDispatcher replaces}
 * it with a {@link MockRequestDispatcher} for the provided
 * {@code defaultServletName}.//ww  w  . j  a  v a  2s  .c o  m
 * @param defaultServletName the name of the <em>default</em> {@code Servlet};
 * never {@code null} or empty
 * @see #getDefaultServletName
 */
public void setDefaultServletName(String defaultServletName) {
    Assert.hasText(defaultServletName, "defaultServletName must not be null or empty");
    unregisterNamedDispatcher(this.defaultServletName);
    this.defaultServletName = defaultServletName;
    registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName));
}