在视图或者 struts 中通过 ServletContext 获取 WebApplicationContext,三个方法如下,英文很简单我就不用再翻译了:
-
getRequiredWebApplicationContext
public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc) throws IllegalStateException
Find the root WebApplicationContext for this web application, which is typically loaded viaContextLoaderListener
.Will rethrow an exception that happened on root context startup, to differentiate between a failed context startup and no context at all.
-
Parameters:
-
sc
- ServletContext to find the web application context for
Returns:
- the root WebApplicationContext for this web app Throws:
-
IllegalStateException
- if the root WebApplicationContext could not be found
See Also:
-
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
-
-
getWebApplicationContext
public static WebApplicationContext getWebApplicationContext(ServletContext sc)
Find the root WebApplicationContext for this web application, which is typically loaded viaContextLoaderListener
.Will rethrow an exception that happened on root context startup, to differentiate between a failed context startup and no context at all.
-
Parameters:
-
sc
- ServletContext to find the web application context for
Returns:
-
the root WebApplicationContext for this web app, or
null
if none
See Also:
-
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
-
-
getWebApplicationContext
public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName)
Find a custom WebApplicationContext for this web application.-
Parameters:
-
sc
- ServletContext to find the web application context for -
attrName
- the name of the ServletContext attribute to look for
Returns:
-
the desired WebApplicationContext for this web app, or
null
if none
-