struts2+hibernate+spring
文章主要讲述如何配置struts2+hibernate+spring,以及进行简单的应用开发
一 web.xml的配置以及简但说明:
web.xml如何配置:
<web-app id="WebApp_9" version="2.4"
xmlns=" http://java.sun.com/xml/ns/j2ee"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- hibernate filter -->
<!-- <filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
简单说明:
首先我们struts2中用到的action的创建工作是由spring 来完成的。spring提供一个监听器-org.springframework.web.context.ContextLoaderListener. 该监听其实现了ContextLoaderListener接口。servletContext的初始化事件以及销毁事件都会被其监听到!web.xml中定义了如下元素
(1)定义一个监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
(2)监听器需要知道AppContext配置文件的位置
于是context-param 中定义了一个参数:contextConfigLocation。spring通过参数contextConfigLocation 可以得到applicationContext配置文件的位置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
在struts1.x中所有以 web.xml中定义了一个servelet----。所有以.do结为的请求都交与 该servlet处理。该servlet会根据根据用户请求,来调度不同的aciton进行处理。但是在struts2中,所有的请求都直接通过org.apache.struts2.dispatcher.FilterDispatcher来进行调度。
该类在初始化时创建一个默认的Dispatcher实例---org.apache.struts2.dispatcher.Dispatcher。该实例负责加载action类,并且调用用户配置的aciton方法(默认调用excute);方法向VALUESTACK中压入view试图(jsp界面)中所需要的值