Example usage for org.springframework.web.servlet DispatcherServlet WEB_APPLICATION_CONTEXT_ATTRIBUTE

List of usage examples for org.springframework.web.servlet DispatcherServlet WEB_APPLICATION_CONTEXT_ATTRIBUTE

Introduction

In this page you can find the example usage for org.springframework.web.servlet DispatcherServlet WEB_APPLICATION_CONTEXT_ATTRIBUTE.

Prototype

String WEB_APPLICATION_CONTEXT_ATTRIBUTE

To view the source code for org.springframework.web.servlet DispatcherServlet WEB_APPLICATION_CONTEXT_ATTRIBUTE.

Click Source Link

Document

Request attribute to hold the current web application context.

Usage

From source file:ar.com.zauber.commons.web.uri.assets.AssetsTest.java

/** create request */
private MockHttpServletRequest createRequest(final XmlWebApplicationContext ctx) {
    final MockHttpServletRequest request = new MockHttpServletRequest("GET",
            "/foo/organizations/zauber/projects");
    request.setContextPath("/foo");
    // esto se requiere para que funcione el buscar el ctx dado un request 
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx);
    final ServletRequestAttributes attributes = new ServletRequestAttributes(request);
    request.setAttribute(RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES", attributes);
    LocaleContextHolder.setLocale(request.getLocale());
    RequestContextHolder.setRequestAttributes(attributes);
    return request;
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerViewTests.java

@Test
public void testKeepExistingContentType() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();

    MockControl wmc = MockControl.createNiceControl(WebApplicationContext.class);
    WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
    MockServletContext sc = new MockServletContext();

    wac.getBeansOfType(FreeMarkerConfig.class, true, false);
    Map configs = new HashMap();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new FreeMarkerTestConfiguration());
    configs.put("configurer", configurer);
    wmc.setReturnValue(configs);//from ww w.  j a  v  a2s . c o m
    wac.getParentBeanFactory();
    wmc.setReturnValue(null);
    wac.getServletContext();
    wmc.setReturnValue(sc, 5);
    wmc.replay();

    fv.setUrl("templateName");
    fv.setApplicationContext(wac);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.US);
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    HttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("myContentType");

    Map model = new HashMap();
    model.put("myattr", "myvalue");
    fv.render(model, request, response);

    wmc.verify();
    assertEquals("myContentType", response.getContentType());
}

From source file:ch.ralscha.extdirectspring.bean.ExtDirectResponseBuilderTest.java

private MockHttpServletRequest createRequest() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    request.setParameter("extAction", "action");
    request.setParameter("extMethod", "method");
    request.setParameter("extType", "type");
    request.setParameter("extTID", "1");
    return request;
}

From source file:org.openmrs.contrib.metadatarepository.webapp.taglib.LabelTag.java

/**
 * Get the validator resources from a ValidatorFactory defined in the
 * web application context or one of its parent contexts.
 * The bean is resolved by type (org.springframework.validation.commons.ValidatorFactory).
 *
 * @return ValidatorResources from a ValidatorFactory.
 *///from w ww  .  java 2  s  . com
private ValidatorResources getValidatorResources() {
    // look in servlet beans definition (i.e. action-servlet.xml)
    WebApplicationContext ctx = (WebApplicationContext) pageContext.getRequest()
            .getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    ValidatorFactory factory = null;
    try {
        factory = (ValidatorFactory) BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, ValidatorFactory.class,
                true, true);
    } catch (NoSuchBeanDefinitionException e) {
        // look in main application context (i.e. applicationContext.xml)
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext());
        factory = (ValidatorFactory) BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, ValidatorFactory.class,
                true, true);
    }
    return factory.getValidatorResources();
}

From source file:com.javaetmoi.core.mvc.tag.TestHtml5InputTag.java

protected MockPageContext createPageContext() {
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);/*www  . ja  v a2s. c  om*/
    wac.setNamespace("test");
    wac.registerSingleton("validator",
            org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.class);
    wac.refresh();

    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    LocaleResolver lr = new AcceptHeaderLocaleResolver();
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
    ThemeResolver tr = new FixedThemeResolver();
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
    request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);

    return new MockPageContext(sc, request, response);
}

From source file:alpha.portal.webapp.taglib.LabelTag.java

/**
 * Get the validator resources from a ValidatorFactory defined in the web
 * application context or one of its parent contexts. The bean is resolved
 * by type (org.springframework.validation.commons.ValidatorFactory).
 * /*from   ww  w .  j  a v a  2s  . co  m*/
 * @return ValidatorResources from a ValidatorFactory.
 */
private ValidatorResources getValidatorResources() {
    // look in servlet beans definition (i.e. action-servlet.xml)
    WebApplicationContext ctx = (WebApplicationContext) this.pageContext.getRequest()
            .getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    ValidatorFactory factory = null;
    try {
        factory = BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, ValidatorFactory.class, true, true);
    } catch (final NoSuchBeanDefinitionException e) {
        // look in main application context (i.e. applicationContext.xml)
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.pageContext.getServletContext());
        factory = BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, ValidatorFactory.class, true, true);
    }
    return factory.getValidatorResources();
}

From source file:org.springmodules.validation.commons.taglib.JavascriptValidatorTag.java

/**
 * Get the validator resources from a ValidatorFactory defined in the
 * web application context or one of its parent contexts.
 * The bean is resolved by type (org.springmodules.commons.validator.ValidatorFactory).
 *
 * @return ValidatorResources from a ValidatorFactory
 *///from   w  w  w  .j  a v  a2 s .co  m
private ValidatorResources getValidatorResources() {
    WebApplicationContext ctx = (WebApplicationContext) pageContext.getRequest()
            .getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (ctx == null) {
        // look in main application context (i.e. applicationContext.xml)
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext());
    }
    ValidatorFactory factory = (ValidatorFactory) BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx,
            ValidatorFactory.class, true, true);
    return factory.getValidatorResources();
}

From source file:org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.java

@Nullable
private static WebApplicationContext getWebApplicationContext() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes == null) {
        logger.debug("No request bound to the current thread: not in a DispatcherServlet request?");
        return null;
    }/* w  ww . ja v  a  2s.  c om*/

    HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
    WebApplicationContext wac = (WebApplicationContext) request
            .getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (wac == null) {
        logger.debug("No WebApplicationContext found: not in a DispatcherServlet request?");
        return null;
    }
    return wac;
}