您的位置:首页 > 编程语言 > Java开发

(12)SSH整合中的web.xml文件

2016-07-28 11:56 411 查看
在进行SSH整合时,web.xml中需要引入Struts和Spring。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name>

<!-- 配置spring的OpenSessionInView模式 【目的:JSp页面访问懒加载数据】 -->
<!-- 注意:访问struts时候需要带上*.action后缀 -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

<!-- 1. struts配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 2. spring 配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/bean-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


其中涉及到OpenSessionInViewFilter、StrutsPrepareAndExecuteFilter和ContextLoaderListener。

OpenSessionInViewFilter

全名:org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

Servlet 2.3 Filter that binds a Hibernate Session to the thread for the entire processing of the request. Intended for the "Open Session in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed.

This filter makes Hibernate Sessions available via the current thread, which will be auto detected by transaction managers. It is suitable for service layer transactions via org.springframework.orm.hibernate3.HibernateTransactionManager.

OpenSessionInViewInterceptor

全名:org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
Spring web request interceptor that binds a Hibernate Session to the thread for the entire processing of the request.

This class is a concrete expression of the "Open Session in View" pattern,which is a pattern that allows for the lazy loading of associations in web views despite the original transactions already being completed.

This interceptor makes Hibernate Sessions available via the current thread, which will be auto detected by transaction managers. It is suitable for service layer transactions via org.springframework.orm.hibernate3.HibernateTransactionManager.

StrutsPrepareAndExecuteFilter

全名:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
Handles both the preparation and execution phases of the Struts dispatching process. This filter is better to use when you don't have another filter that needs access to action context information, such as Sitemesh.

StrutsPrepareFilter

全名:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter
Prepares the request for execution by a later org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter filter instance.

StrutsExecuteFilter

全名:org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter
Executes the discovered request information. This filter requires the StrutsPrepareFilter to have already been executed in the current chain.

ContextLoaderListener

全名:org.springframework.web.context.ContextLoaderListener
Bootstrap listener to start up and shut down Spring's root Web Application Context. Simply delegates to ContextLoader as well as to ContextCleanupListener.

This listener should be registered after org.springframework.web.util.Log4jConfigListener in web.xml, if the latter is used.
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

private ContextLoader contextLoader;

/**
* Create a new {@code ContextLoaderListener} that will create a web application
* context based on the "contextClass" and "contextConfigLocation" servlet
* context-params. See {@link ContextLoader} superclass documentation for details on
* default values for each.
* <p>This constructor is typically used when declaring {@code ContextLoaderListener}
* as a {@code <listener>} within {@code web.xml}, where a no-arg constructor is
* required.
* <p>The created application context will be registered into the ServletContext under
* the attribute name {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE}
* and the Spring application context will be closed when the {@link #contextDestroyed}
* lifecycle method is invoked on this listener.
* @see ContextLoader
* @see #ContextLoaderListener(WebApplicationContext)
* @see #contextInitialized(ServletContextEvent)
* @see #contextDestroyed(ServletContextEvent)
*/
public ContextLoaderListener() {
}
}


ServletContextListener

全名:javax.servlet.ServletContextListener
Implementations of this interface receive notifications about changes to the servlet context of the web application they are part of. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.
public interface ServletContextListener extends EventListener {
/**
** Notification that the web application initialization
** process is starting.
** All ServletContextListeners are notified of context
** initialization before any filter or servlet in the web
** application is initialized.
*/

public void contextInitialized ( ServletContextEvent sce );

/**
** Notification that the servlet context is about to be shut down.
** All servlets and filters have been destroy()ed before any
** ServletContextListeners are notified of context
** destruction.
*/
public void contextDestroyed ( ServletContextEvent sce );
}


ContextLoader

全名:org.springframework.web.context.ContextLoader

ContextLoader类执行root applicationcontext的实例化工具,它被ContextLoaderListener调用。
Performs the actual initialization work for the root application context. Called by ContextLoaderListener.
ContextLoader会在web.xml中查找名为”contextClass”的context-param参数,如果这个参数不存在,就使用默认的org.springframework.web.context.support.XmlWebApplicationContext。
Looks for a "contextClass" parameter at the web.xml context-param level to specify the context class type,falling back to the default of org.springframework.web.context.support.XmlWebApplicationContext if not found.With the default ContextLoader implementation, any context class specifiedneeds to implement the ConfigurableWebApplicationContext interface.
ContextLoader接着处理名为“contextConfigLocation”的context-param参数,将相应的值传递给applicationcontext实例。“contextConfigLocation”的值中如果包含多个值,可以用commas或space进行分隔。另外,“contextConfigLocation”中也支持Ant-style path pattern。

Processes a"contextConfigLocation" context-param and passes its value to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, e.g."WEB-INF/applicationContext1.xml, WEB-INF/applicationContext2.xml".Ant-style path patterns are supported as well, e.g."WEB-INF/*Context.xml,WEB-INF/spring*.xml" or "WEB-INF/**/*Context.xml".If not explicitly specified, the context implementation is supposed to use a default location (with XmlWebApplicationContext:"/WEB-INF/applicationContext.xml").
public class ContextLoader {

/**
* Config param for the root WebApplicationContext implementation class to use: "contextClass"
*/
public static final String CONTEXT_CLASS_PARAM = "contextClass";

/**
* Name of servlet context parameter (i.e., "contextConfigLocation") that can specify the
* config location for the root context, falling back to the implementation's default otherwise.
*
* @see org.springframework.web.context.support.XmlWebApplicationContext.DEFAULT_CONFIG_LOCATION
*/
public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

/**
* The 'current' WebApplicationContext, if the ContextLoader class is
* deployed in the web app ClassLoader itself.
*/
private static volatile WebApplicationContext currentContext;

/**
* The root WebApplicationContext instance that this loader manages.
*/
private WebApplicationContext context;

/**
* Create a new {@code ContextLoader} that will create a web application context
* based on the "contextClass" and "contextConfigLocation" servlet context-params.
*
* <p>This constructor is typically used when declaring the {@code
* ContextLoaderListener} subclass as a {@code <listener>} within {@code web.xml}, as
* a no-arg constructor is required.
*
* <p>The created application context will be registered into the ServletContext under
* the attribute name {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE}
* and subclasses are free to call the {@link #closeWebApplicationContext} method on
* container shutdown to close the application context.
* @see #ContextLoader(WebApplicationContext)
* @see #initWebApplicationContext(ServletContext)
* @see #closeWebApplicationContext(ServletContext)
*/
public ContextLoader() {
}

/**
* Initialize Spring's web application context for the given servlet context,
* using the application context provided at construction time, or creating a new one
* according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and
* "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.
* @param servletContext current servlet context
* @return the new WebApplicationContext
* @see #ContextLoader(WebApplicationContext)
* @see #CONTEXT_CLASS_PARAM
* @see #CONFIG_LOCATION_PARAM
*/
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}

// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

return this.context;
}

/**
* Close Spring's web application context for the given servlet context.
*
* @param servletContext the ServletContext that the WebApplicationContext runs in
*/
public void closeWebApplicationContext(ServletContext servletContext) {

if (this.context instanceof ConfigurableWebApplicationContext) {
((ConfigurableWebApplicationContext) this.context).close();
}

servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

}

/**
* Obtain the Spring root web application context for the current thread.
*
* @return the current root web application context, or {@code null}
* if none found
* @see org.springframework.web.context.support.SpringBeanAutowiringSupport
*/
public static WebApplicationContext getCurrentWebApplicationContext() {
return currentContext;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java SSH