<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 采用通配符方式,需要关闭DynamicMethodInvocation 为 false--> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <package name="manager" extends="default"> <!--自定义拦截器配置 --> <interceptors> <interceptor name="loginInterceptor" class="com.hrbkejia.interceptor.LoginInterceptor"/> </interceptors> <!-- "用户登录Action" "*_*"<<<<这里使用的是通配符 --> <action name="*_*" class="{1}" method="{2}"> <result name="login">/WEB-INF/jsp/UserLogin.jsp</result> <result name="success">/WEB-INF/jsp/main.jsp</result> <!-- <interceptor-ref name="defaultStack"/> --> <!-- struts 内置的默认拦截器 --> <!--<interceptor-ref name="loginInterceptor"/> --> <!-- struts 内置的默认拦截器 --> </action> </package> </struts>
前提是struts2 和hibernate 已经已成。
第一步要引入spring 需要的jar包,我这里使用的sping2.0 ,在dist文件夹下引入spring.jar 和 \lib\c3p0下的
c3p0-0.9.0.4.jar。
第二步、改变Dao层数据库访问方法,采用spring的方法,看一下DaoUserLogin.java
package com.hrbkejia.dao.impl; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.orm.hibernate3.HibernateCallback; import com.hrbkejia.bean.UserManager; import com.hrbkejia.dao.UserManagerDao; import com.hrbkejia.util.HibernateSessionFactory; public class UserManagerDaoImpl extends HibernateDaoSupport implements UserManagerDao{ /** * 按照用户名和密码查询登录用户实例 */ public UserManager queryUserManager(final UserManager userManager) throws Exception { List list = this.getHibernateTemplate().find( "from com.hrbkejia.bean.UserManager as user where user.userName=? and user.passWord=?", new String[]{userManager.getUserName(),userManager.getPassWord()}); if(list != null && list.size() >= 1){ return (UserManager)list.get(0); } return null; } public static void main(String [] args){ UserManagerDaoImpl s = new UserManagerDaoImpl(); UserManager userManager = new UserManager(); userManager.setUserName("admin"); userManager.setPassWord("admin"); try { //s.saveUserManager(userManager); UserManager u = (UserManager)s.queryUserManager(userManager); System.out.println(u.getUserName()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean saveUserManager(final UserManager userManager) throws Exception { try{ this.getHibernateTemplate().save(userManager); }catch(Exception e){ if(e != null){ e.printStackTrace(); return false; } } return true; } public List queryAllUserManager() throws Exception { return (List<UserManager>)this.getHibernateTemplate().find( "from com.hrbkejia.bean.UserManager as user "); } public List<UserManager> queryAllUserManager(final int startNum, final int endNum) throws Exception { return (List<UserManager>)getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createQuery("from com.hrbkejia.bean.UserManager"); query.setFirstResult(startNum); query.setMaxResults(endNum); List list = query.list(); return list; } }); } public boolean deleteUserManager(UserManager userManager) throws Exception { try{ this.getHibernateTemplate().delete(userManager); }catch(Exception e){ if(e != null){ e.printStackTrace(); return false; } } return true; } public boolean updateUserManager(UserManager userManager) throws Exception { try{ this.getHibernateTemplate().update(userManager); }catch(Exception e){ if(e != null){ e.printStackTrace(); return false; } } return true; } public boolean updateOrSaveUserManager(UserManager userManager) throws Exception { try{ this.getHibernateTemplate().saveOrUpdate(userManager); }catch(Exception e){ if(e != null){ e.printStackTrace(); return false; } } return true; } public int getCount() throws Exception { List list = this.getHibernateTemplate().find("select count(*) from UserManager"); if(list != null && list.size() >=0){ return Integer.parseInt(list.get(0).toString()); } return 0; } }
创建webRoot/WEB-INF/下创建applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 采用c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/×××××××"/> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="user" value="root"/> <property name="password" value="×××"/> <!-- 配置数据库连接池,最大连接数 --> <property name="maxPoolSize" value="20"/> <!-- 配置数据库连接池,最小连接数 --> <property name="minPoolSize" value="1"/> <!-- 指定数据库连接池的初始化连接数 --> <property name="initialPoolSize" value="1"/> <!-- 指定数据库连接池的连接的最大空闲时间 --> <property name="maxIdleTime" value="10"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>com/hrbkejia/bean/UserManager.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <!-- 采用MySql作为数据库系统,故使用MySQL方言 <prop key="hibernate.hbm2ddl.auto">update</prop> --> <prop key="show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">crate</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 继承HibernateDaoSupport类,setSessionFactory()在HibernateDaoSupport类中已经做定义,所以不用重写setSessionFactory() --> <bean id="userManagerDaoBean" class="com.hrbkejia.dao.impl.UserManagerDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 用户登录Bean --> <bean id="login" class="com.hrbkejia.Action.UserLoginAction" scope="prototype" > <property name="userManagerDao" ref="userManagerDaoBean"/> </bean> </beans>
在web.xml 添加启动spring 监听web.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- <display-name>Struts Blank</display-name> --> <listener> <!-- spring2.0 监听--> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
接下来引入struts2 对 spring 支持的插件“struts2-spring-plugin-2.3.15.3.jar ”
接下来配置struts2.xml文件,这里使用的通配符