struts2+hibernate+spring整合

来源:互联网 发布:谷嫂的淘宝店 编辑:程序博客网 时间:2024/05/02 04:42

applicationContext-db.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-2.0.xsd">


 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="com.mysql.jdbc.Driver">
  </property>
  <property name="url" value="jdbc:mysql://localhost:3306/cou"></property>
  <property name="username" value="root"></property>
  <property name="password" value="xin"></property>
 </bean>
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">
     true
    </prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
   <value>com/model/Admin.hbm.xml</value>

   </list>
  </property>
 </bean>
 
</beans>

 

applicationContext-dao.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-2.0.xsd">
 
 <bean id="adminDAO" class="com.hibernate.admin.impl.AdminDAOImpl">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean> 
   
</beans>

 

applicationContext-serv.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-2.0.xsd">
 
 <bean id="adminServ" class="com.service.impl.AdminServImpl">
  <property name="adminDAO">
   <ref bean="adminDAO"/>
  </property>
 </bean>

</beans>

 

applicationContext-action.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-2.0.xsd">
 
 <bean id="loginAction" class="com.action.LoginAction">
  <property name="adminServ">
   <ref bean="adminServ"/>
  </property>
 </bean>
 
</beans>

 

web.xml文件为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext-*.xml</param-value>
 </context-param>

 

 <filter>
  <filter-name>struts-cleanup</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.ActionContextCleanUp
  </filter-class>
 </filter>

 <filter>
  <filter-name>struts</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>
 
 <filter-mapping>
  <filter-name>struts-cleanup</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping>

 <filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping>
 
 <filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>*.jsp</url-pattern>
 </filter-mapping>

   <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
  
 </web-app>

 

说明:<param-value>/WEB-INF/applicationContext-*.xml</param-value>把所有applicationContext-*.xml这种格式的包括进来。

 

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.objectFactory" value="spring"/>
 <constant name="struts.multipart.saveDir" value="/tmp"></constant>
 <include file="struts-default.xml"/>
 
 <package name="admin" extends="struts-default">
  <action name="login" class="loginAction" method="login">
   <result name="success" type="redirect">/admin/index.jsp</result>
   <result name="error">/error.jsp</result>
  </action>
 </package>

</struts>

 

说明:<constant name="struts.objectFactory" value="spring"/>用来表示Struts由spring来管理

 

部分代码:(持久类中必须继承HibernateDaoSupport这个类)

public Admin getAdmin(final String account,final String password){
  Admin admin=null;
  final String hql="from Admin admin where admin.adminAccount=:account and admin.adminPassword=:password";
  /*List list=getHibernateTemplate().find(hql,new Object[]{username,password});
  if(!list.isEmpty()){
   admin=(Admin)list.get(0);
  }
  return admin;*/
  return (Admin)getHibernateTemplate().execute(
    new HibernateCallback(){
     public Object doInHibernate(Session session)throws HibernateException,SQLException{
      Query query=session.createQuery(hql).setString("account",account).setString("password",password);
      return (Admin)query.uniqueResult();
     }
    });
 }

 

注:由set方法注入依赖,因此必须实习get/set方法才可以注入

 

最后就是jsp页面,action调用service,service调用hibernate(持久类)。

原创粉丝点击