江西广告网

标题: SSH整合方案之一 [打印本页]

作者: 早妖的夏天    时间: 2009-2-17 10:58
标题: SSH整合方案之一
SSH(方案一) 1,导入spring,包含(ormDAO包)(web包) 2,导入hibernate,将hibernate的信息到写到Spring的applicationContext.xml中,使hibernate受spring管理, 配置如: <?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="run" class="com.sk.run.RunHiber"><!—RunHiber类继承了HibernateDAOSupport抽象类,此类中有HibernateTemplate属性,so注入 --> <property name="hibernateTemplate"> <ref bean="myHT"/> </property> </bean> <bean id="myHT" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" > <ref bean="mySessionFactory"/> </property> </bean> <bean id="myDatasource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value> </property> <property name="url"> <value>jdbc:microsoft:sqlserver://127.0.0.1:1433</value> </property> <property name="username"> <value>sa</value> </property> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="myDatasource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>com/sk/bean/Student.hbm.xml</value> </list> </property> </bean> </beans> 3,导入struts()修改web.xml,添加spring的路径和监听 配置如: [1] [2] [3] <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 指定applicationContext的位置其必须的监听器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> 4.所有的action都继承 ActionSupport类(此类是action的扩展类) 用WebApplicationContext 获取 spring管理的bean 代码如: public class LoginAction extends ActionSupport { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub String name=loginForm.getName(); String password=loginForm.getPassword(); WebApplicationContext context=this.getWebApplicationContext(); RunHiber r=(RunHiber)context.getBean("run"); if(r.getStudent(name, password)!=null){ return mapping.findForward("succ"); } return null; } } 上一页 [1] [2] [3] Run类的内容: package com.sk.run; import java.util.Iterator; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.sk.bean.Student; public class RunHiber extends HibernateDaoSupport{ public Student getStudent(String name,String password){ HibernateTemplate t= this.getHibernateTemplate(); String hql="from Student as s where s.username=?"; List list=t.find(hql, name); Iterator i=list.iterator(); while(i.hasNext()){ Student s=(Student)i.next(); if(s.getPassword().equals(password)){ return s; } } return null; } public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); RunHiber r=(RunHiber)context.getBean("run"); r.getStudent("aaa", "aaa"); } } 上一页 [1] [2] [3]




欢迎光临 江西广告网 (http://bbs.jxadw.com/) Powered by Discuz! X3.2