方式1:
1在struts-config.xml中所有的action的class全部写成org.springframework.web.struts.DelegatingActionProxy
<action parameter="method" path="/users"
type="org.springframework.web.struts.DelegatingActionProxy" >
<forward name="login" path="/WEB-INF/jsp/login.jsp" />
</action>
2在spring.xml中配置action
<bean name="/users" class="com.struts.struts.action.UsersAction">
<property name="dao" ref="UsersDAO"></property>
</bean>
3在struts-config.xml文件中配置plug-in
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="WEB-INF/classes/com/xaccp/config/action-servlet.xml,WEB-INF/classes/com/xaccp/config/spring.xml"/>
</plug-in>
方式2:
1 在web.xml中的<web-app>节点中加上
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/com/xaccp/config/spring.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
2 配置struts-config.xml中的
<action parameter="method" path="/users"
type="org.springframework.web.struts.DelegatingActionProxy" >
<forward name="login" path="/WEB-INF/jsp/login.jsp" />
</action>
3 在spring.xml中配置
<bean name="/users" class="com.struts.struts.action.UsersAction">
<property name="dao" ref="UsersDAO"></property>
</bean>
方式3:
在src下新建一个包如com.xaccp.config中放置action-servlet.xml,hibernate.cfg.xml,spring.xml
三个文件
在action-servlet.xml文件中全部放置真正的action的包名和类名,其中完成IOC的注入
如:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="/users" class="com.struts.struts.action.UsersAction">
<property name="dao" ref="UsersDAO"></property>
</bean>
</beans>
在hibernate.cfg.xml中放置所有数据库的设置
如
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=HR_DB
</property>
<property name="connection.username">sa</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="myeclipse.connection.profile">DBM</property>
<property name="show_sql">true</property>
<mapping resource="com/xaccp/po/Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
在spring.xml中配置DAO和事务等
如:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="session"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>WEB-INF/classes/com/xaccp/config/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="jndi" class="com.xaccp.jndidemo.jnditest"></bean>
<bean id="dao" class="com.xaccp.dao.loginDao">
<property name="jndi" ref="jndi"></property>
</bean>
<bean id="UsersDAO" class="com.xaccp.dao.UsersDAO">
<property name="sessionFactory">
<ref bean="session" />
</property>
</bean>
</beans>
最后在struts-config.xml中设置action和plug-in
如:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards>
<forward name="ok" path="/ok.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
</global-forwards>
<action-mappings >
<action
parameter="method"
path="/users"
type="org.springframework.web.struts.DelegatingActionProxy" >
<forward name="login" path="/WEB-INF/jsp/login.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.xaccp.struts.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="WEB-INF/classes/com/xaccp/config/action-servlet.xml,WEB-INF/classes/com/xaccp/config/spring.xml"/>
</plug-in>
</struts-config>
其中在spring.xml中的session<bean id="session"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>WEB-INF/classes/com/xaccp/config/hibernate.cfg.xml</value>
</property>
</bean>
不用再在spring.xml中配置数据源和sessionFactory了,可以直接使用hibernate.cfg.xml配置的信息
分享到:
相关推荐
Struts Spring Hibernate整合实践
struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页
struts spring hibernate整合(二) struts spring hibernate整合(二)struts spring hibernate整合(二)
struts spring hibernate整合(三) struts spring hibernate整合(三) struts spring hibernate整合(三)
Struts Spring Hibernate整合实践.doc
struts spring hibernate整合
Struts Spring Hibernate 整合教程
struts spring hibernate 整合实现方案
使用mysql数据库的struts2 spring hibernate整合简单示例,也可参考我的:“struts2 spring hibernate整合要点、注意点”这篇文档来配置
Struts技术资料 hibernate技术资料 spring技术资料 ssh整合技术(struts与hibernate整合,struts与spring整合,hibernate与spring整合,struts,hibernate,spring整合)
struts2 spring hibernate 整合
Struts Spring Hibernate 整合引用2008
绝对详细的整合教材,值得你们拥有!!!目前流行的框架
struts spring hibernate 登陆 SSH整合登陆功能struts spring hibernate 登陆 SSH整合登陆功能struts spring hibernate 登陆 SSH整合登陆功能
Struts Spring Hibernate 整合开发
一个完整的struts spring hibernate整合的例子
平台是:MyElipse5.1,Tomcat5.59,Struts1.1,Spring1.2,Hibernate3.0,Sql server2000.