1、可以参考struts的docs文档spring-plugin.html。
2、也可以参考spring的帮助文档的15.4节struts。
- Configure the Spring listener
web.xml
org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:beans.xml
默认的路径是>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml,也可以把spring的配置文件放到src目录下,配置就是classpath:beans.xml。其中beans.xml是文件名。
原帮助文档的描述:
applicationContext.xml
...
More applicationContext configuration files needed? Since the Spring integration uses a standard Listener, it can be configured to support configuration files other than applicationContext.xml. Adding the following to your web.xml will cause Spring's ApplicationContext to be inititalized from all files matching the given pattern:
See the Spring documentation for a full description of this parameter. |
注意:
如果beans.xml文件放在src目录下,在生成ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");但若是applicationContext.xml必须放到WEB-INF下的Classes目录下,才能用ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");生成ApplicationContext,若是直接放到WEB-INF下会抛出异常说找不到applicationContext文件
在Spring配置文件applicationContext.xml中加入数据库连接配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ""> <beans> <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> <property name="environment"> <props> <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop> <prop key="java.naming.provider.url">t3://127.0.0.1:7001</prop> <prop key="java.naming.security.principal">weblogic</prop> <prop key="java.naming.security.credentials">weblogic</prop> </props> </property> </bean><!--利用Weblogic配置数据库jndi连接-->
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>MySQLDataSource</value> </property> <property name="jndiTemplate"> <ref local="jndiTemplate" /> </property> </bean><!--直接配置数据库连接-->
< bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/test</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>123</value> </property> </bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"> <ref bean="jndiDataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop> </props> </property> <!-- property name="mappingResources"> <list> <value>cn/git/common/hbm/UserInfo.hbm.xml</value> </list> </property--> <property name="mappingDirectoryLocations"> <list> <value>classpath:/cn/git/common/hbm</value> </list> </property><!--配置Spring中的Hibernate模板类-->
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean><!--在类中直接调用hibernate模板类来访问数据库-->
<bean id="systemLoginDAO" class="cn.git.systemLogin.dao.hibernate.SystemLoginDAO"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate" /> </property> </bean></beans>
配置完成后即可在systemLoginDAO类中直接访问数据库了:)
package cn.git.systemLogin.dao.hibernate;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.ResultSet;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.classic.Session;import cn.git.systemLogin.dao.ISystemLoginDAO;
public class SystemLoginDAO extends HibernateDaoSupport implements
ISystemLoginDAO ...{ private Log log=LogFactory.getLog(this.getClass()); public boolean checkUserPassword(String userName,String userPassword)...{ Session session=getHibernateTemplate().getSessionFactory().openSession(); Connection conn = session.connection(); String query="select * from userInfo where userName=? and userPassword=?"; try...{ PreparedStatement pstmt = conn.prepareStatement(query); pstmt.setString(1,userName); pstmt.setString(2,userPassword); ResultSet rs = pstmt.executeQuery(); if(rs.next())...{ return true; }else...{ return false; } } catch(Exception e)...{ return false; }finally...{ session.close(); } } }