2.集成Spring和Hibernate
1) Struts2和Spring2集成
1.1)将D:\JavaTools\struts-2.1.2\lib\下的
struts2-spring-plugin-2.1.2.jar复制到
D:\Project\Bitrac\WebRoot\WEB-INF\lib\
1.2)在web.xml文件中加入Spring配置:
<!--默认加载/WEB-INF 目录下的applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
加入后完整的文件内容如下:
- <? xml version = " 1.0 " encoding = " UTF-8 " ?>
- < web-app id = " bitrac " 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 " >
- < display-name > Bitrac Blog </ display-name >
- < filter >
- < filter-name > Struts2 </ filter-name >
- < filter-class >
- org.apache.struts2.dispatcher.FilterDispatcher
- </ filter-class >
- </ filter >
- < filter-mapping >
- < filter-name > Struts2 </ filter-name >
- < url-pattern > /* </ url-pattern >
- </ filter-mapping >
- <!--默认加载/WEB-INF 目录下的applicationContext.xml -->
- < 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 >
1.3)新建Struts配置文件Bitrac\src\default.xml,内容如下:
- <? xml version = " 1.0 " encoding = " UTF-8 " ?>
- < ! DOCTYPE struts PUBLIC
- " -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
- " http://struts.apache.org/dtds/struts-2.0.dtd " >
- < struts >
- < package name = " default " extends = " struts-default " >
- < action name = " admincp " >
- < result > /admin/login.jsp </ result >
- </ action >
- </ package >
- </ struts >
这个配置文件将用来存放前台的action配置
1.4)新建Struts配置文件Bitrac\src\admin.xml,内容如下:
- <? xml version = " 1.0 " encoding = " UTF-8 " ?>
- < ! DOCTYPE struts PUBLIC
- " -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
- " http://struts.apache.org/dtds/struts-2.0.dtd " >
- < struts >
- < package name = " admin " extends = " struts-default " namespace = " /admin " >
- < action name = " login " class = " adminAction " method = " login " >
- < result name = " success " > /admin/index.jsp </ result >
- < result name = " input " > /admin/login.jsp </ result >
- </ action >
- </ package >
- </ struts >
这个文件将用来存放后台管理界面的Action配置
1.5)修改Bitrac\src\struts.xml,内容如下:
- <? xml version = " 1.0 " encoding = " UTF-8 " ?>
- < ! DOCTYPE struts PUBLIC
- " -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
- " http://struts.apache.org/dtds/struts-2.0.dtd " >
- < struts >
- < constant name = " struts.devMode " value = " true " />
- < constant name = " struts.configuration.xml.reload " value = " true " />
- < constant name = " struts.action.extension " value = " jspx " />
- < include file = " default.xml " />
- < include file = " admin.xml " />
- </ struts >
这里配置了一些全局常量,struts.action.extentsion是访问action时的后缀,通常用的有action或者do,我的习惯是用jspx,例如/admin/login.jspx,将调用admin.xml里定义的login action。
1.6)新建Spring配置文件
- <? xml version = " 1.0 " encoding = " UTF-8 " ?>
- < beans xmlns = " http://www.springframework.org/schema/beans "
- xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "
- xmlns:aop = " http://www.springframework.org/schema/aop "
- xmlns:tx = " http://www.springframework.org/schema/tx "
- xsi:schemaLocation = "
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " >
- < bean name = " adminAction " class = " com.albertsong.bitrac.action.AdminAction " />
- </ beans >
注 意这里的bean name是adminAction,再看Bitrac\src\admin.xml里的class="adminAction",Spring和 Struts的结合点就在这里,后台处理这一结合的是struts2-spring-plugin-2.1.2.jar。如果不使用spring,在 admin.xml中将是class="com.albertsong.bitrac.action.AdminAction"。