/** * Look for the WebApplicationContext associated with the DispatcherServlet * that has initiated request processing, and for the global context if none * was found associated with the current request. The global context will * be found via the ServletContext or via ContextLoader's current context. * <p>NOTE: This variant requires Servlet 3.0+ and is generally recommended * for forward-looking custom user code. * @param request current HTTP request * @return the request-specific WebApplicationContext, or the global one * if no request-specific context has been found, or {@code null} if none * @since 4.2.1 * @see #findWebApplicationContext(HttpServletRequest, ServletContext) * @see ServletRequest#getServletContext() * @see ContextLoader#getCurrentWebApplicationContext() */ @Nullable public static WebApplicationContext findWebApplicationContext(HttpServletRequest request) { return findWebApplicationContext(request, request.getServletContext()); }
/** * Look for the WebApplicationContext associated with the DispatcherServlet * that has initiated request processing, and for the global context if none * was found associated with the current request. The global context will * be found via the ServletContext or via ContextLoader's current context. * <p>NOTE: This variant requires Servlet 3.0+ and is generally recommended * for forward-looking custom user code. * @param request current HTTP request * @return the request-specific WebApplicationContext, or the global one * if no request-specific context has been found, or {@code null} if none * @since 4.2.1 * @see #findWebApplicationContext(HttpServletRequest, ServletContext) * @see ServletRequest#getServletContext() * @see ContextLoader#getCurrentWebApplicationContext() */ @Nullable public static WebApplicationContext findWebApplicationContext(HttpServletRequest request) { return findWebApplicationContext(request, request.getServletContext()); }
@Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!(RequestContextUtils.findWebApplicationContext(request) instanceof SimpleWebApplicationContext)) { throw new ServletException("Incorrect WebApplicationContext"); } if (!(RequestContextUtils.getLocaleResolver(request) instanceof AcceptHeaderLocaleResolver)) { throw new ServletException("Incorrect LocaleResolver"); } if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) { throw new ServletException("Incorrect Locale"); } return null; }
/** * Find the registered {@link RequestDataValueProcessor}, if any, and allow * it to update the redirect target URL. * @param targetUrl the given redirect URL * @return the updated URL or the same as URL as the one passed in */ protected String updateTargetUrl(String targetUrl, Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { WebApplicationContext wac = getWebApplicationContext(); if (wac == null) { wac = RequestContextUtils.findWebApplicationContext(request, getServletContext()); } if (wac != null && wac.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) { RequestDataValueProcessor processor = wac.getBean( RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class); return processor.processUrl(request, targetUrl); } return targetUrl; }
protected RequestDataValueProcessor getMockRequestDataValueProcessor() { RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class); HttpServletRequest request = (HttpServletRequest) getPageContext().getRequest(); WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request); wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor); return mockProcessor; }
/** * Find the registered {@link RequestDataValueProcessor}, if any, and allow * it to update the redirect target URL. * @param targetUrl the given redirect URL * @return the updated URL or the same as URL as the one passed in */ protected String updateTargetUrl(String targetUrl, Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { WebApplicationContext wac = getWebApplicationContext(); if (wac == null) { wac = RequestContextUtils.findWebApplicationContext(request, getServletContext()); } if (wac != null && wac.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) { RequestDataValueProcessor processor = wac.getBean( RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class); return processor.processUrl(request, targetUrl); } return targetUrl; }
wac = RequestContextUtils.findWebApplicationContext(request, servletContext); if (wac == null) { throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
@Override public void doSomething(HttpServletRequest request) throws ServletException, IllegalAccessException { WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request); if (!(wac instanceof ComplexWebApplicationContext)) { throw new ServletException("Incorrect WebApplicationContext");
wac = RequestContextUtils.findWebApplicationContext(request, servletContext); if (wac == null) { throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
@Test public void nullMessageSource() throws JspException { PageContext pc = createPageContext(); ConfigurableWebApplicationContext ctx = (ConfigurableWebApplicationContext) RequestContextUtils.findWebApplicationContext((HttpServletRequest) pc.getRequest(), pc.getServletContext()); ctx.close(); MessageTag tag = new MessageTag(); tag.setPageContext(pc); tag.setCode("test"); tag.setVar("testvar2"); tag.doStartTag(); assertEquals("Correct doEndTag return value", Tag.EVAL_PAGE, tag.doEndTag()); }
protected MockPageContext createAndPopulatePageContext() throws JspException { MockPageContext pageContext = createPageContext(); MockHttpServletRequest request = (MockHttpServletRequest) pageContext.getRequest(); ((StaticWebApplicationContext) RequestContextUtils.findWebApplicationContext(request)) .registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class); extendRequest(request); extendPageContext(pageContext); RequestContext requestContext = new JspAwareRequestContext(pageContext); pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext); return pageContext; }
/** 获取Spring的应用上下文环境(WebApplicationContext) **/ public WebApplicationContext getApplicationContext() { HttpServletRequest request = getRequest(); if (request == null) return null; // @see RequestContext.initContext() String wacKey = RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE; WebApplicationContext wac = (WebApplicationContext) request.getAttribute(wacKey); if (wac == null) { wac = RequestContextUtils.findWebApplicationContext(request, request.getServletContext()); if (wac != null) { return wac; } } return null; }
/** 获取Spring的应用上下文环境(WebApplicationContext) **/ public WebApplicationContext getApplicationContext() { HttpServletRequest request = getRequest(); if (request == null) return null; // @see RequestContext.initContext() String wacKey = RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE; WebApplicationContext wac = (WebApplicationContext) request.getAttribute(wacKey); if (wac == null) { wac = RequestContextUtils.findWebApplicationContext(request, request.getServletContext()); if (wac != null) { return wac; } } return null; } }
/** * Builds and writes the response into the OutputStream of {@link HttpServletResponse} * . This methods has to be called at the end of a FORM_POST method. */ public void buildAndWrite() { try { RouterController routerController = RequestContextUtils .findWebApplicationContext(this.request) .getBean(RouterController.class); routerController.writeJsonResponse(this.request, this.response, this.extDirectResponse, this.jsonView); } catch (IOException e) { LogFactory.getLog(getClass()).error("buildAndWrite", e); throw new RuntimeException(e); } }
@Override public String get() { HttpServletRequest request = RequestUtils.getCurrentRequest(); WebApplicationContext applicationContext = RequestContextUtils.findWebApplicationContext(request); return applicationContext.getMessage(resolvableTitle, request.getLocale()); } }
ApplicationContext ctx = RequestContextUtils.findWebApplicationContext(req); Object bean; try {
public String getResource(String resourceKey, String defaultValue, Tag tag, PageContext pageContext) MessageSource messageSource = RequestContextUtils.findWebApplicationContext((HttpServletRequest) pageContext.getRequest()); if (messageSource == null)
/** * Find the registered {@link RequestDataValueProcessor}, if any, and allow * it to update the redirect target URL. * @param targetUrl the given redirect URL * @return the updated URL or the same as URL as the one passed in */ protected String updateTargetUrl(String targetUrl, Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { WebApplicationContext wac = getWebApplicationContext(); if (wac == null) { wac = RequestContextUtils.findWebApplicationContext(request, getServletContext()); } if (wac != null && wac.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) { RequestDataValueProcessor processor = wac.getBean( RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class); return processor.processUrl(request, targetUrl); } return targetUrl; }
wac = RequestContextUtils.findWebApplicationContext(request, servletContext); if (wac == null) { throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
.findWebApplicationContext(this.request); Configuration configuration; try {