练手在eclipse中搭建spring+struts+hibernate开发框架

由于本身项目需要,刚开始接触java,之前也没涉及过框架的开发搭建,所以这里参考论坛和书本搭建一下。

SSH框架是最常用的框架之一,在搭建SSH框架的时候总有人遇到这样,那样的问题。下面我介绍一下SSH框架搭建的全过程。 
第一步:准备工作。 
  下载好eclipse,Struts2,Spring,Hibernate。 
  1.eclipse:eclipse下载的时候建议下载JavaEE版的eclipse。
                 当然你也可以下载eclipse-SDK。(下载eclipse-SDK需要下载Web,Tomcat等plugins) 

Tomcat和JDK的下载配置这里不再详细介绍,网上搜一下有很多可以参考借鉴。
  2.Struts2:http://struts.apache.org/download 
         1)引入Struts的jar包。下载 struts-*-all.zip 解压后,struts/lib目录下是struts所有的相关jar包。 
         其中有5个是必须的:

               Commons-logging-1.0.4.jar,Freemarker-2.3.13.jar, 
               Ognl-2.6.11.jar,Struts2-core-2.1.6.jar,Xwork-2.1.2.jar 
         其余jar包并不是struts必须的。还有3个包也要注意导入。不导入运行Tomcat时候可能会出现异常。 
               commons-io-1.3.2.jar,commons-fileupload-1.2.1.jar,javassist-3.7.ga.jar 
         注意:javassist-3.7.ga.jar包是在struts2-blank-2.2.1.war示例工程中的web-inf/lib下的。 


  3.Spring:http://www.springsource.com/download/community 
        还可以在eclipse下安装下载。具体步骤是这样的:
        1)打开eclipse-help-Software Updates.

 
        2) 在打开的对话框中选择上面的第二项(Available Software)。

 
        3)点击Add Site按钮,弹出URL对话框。 


        4)在对话框里输入:http://springide.org/updatesite/点击OK。 


        5)选择sping IDE点击安装(Install)。


  4.Hibernate:http://sourceforge.net/projects/hibernate/files/hibernate3/ 

  5.Jdk的src.zip包导入。(当然不导入也可以。。。) 

第二步: 

  1.创建一个 Web Progect,自己起一个喜欢的名字。 

  2.修改WEB-INF下的web.xml文件,增加struts2的配置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<display-name>Shopsystem</display-name>
  	<!-- structs framework -->
  	<filter>
  	    <filter-name>structs2</filter-name>
  	    <filter-class>
  	    	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	    </filter-class>
  	</filter>
	<filter-mapping>
	    <filter-name>structs2</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- welcome file -->
    <welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
    
</web-app>

 3.在WEB-INF/classes目录下添加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>
    <package namespace="/" name="struts2" extends="struts-default">
        <action name="login" method="execute" class="loginAction">
            <result name="success">/WEB-INF/jsp/login.jsp</result>
            <result name="input">/WEB-INF/index.jsp</result>
        </action>
    </package>
</struts>

4.配置Spring 

    1)导入spring包。spring-framework-**.zip解压后,将spring-framework-**文件夹的dist目录下的jar包导入工程中。

    2)配置web.xml文件。 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<display-name>Shopsystem</display-name>
  	<!-- structs framework -->
  	<filter>
  	    <filter-name>structs2</filter-name>
  	    <filter-class>
  	    	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	    </filter-class>
  	</filter>
	<filter-mapping>
	    <filter-name>structs2</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- welcome file -->
    <welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
    <!-- spring framework -->
    <listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>
	        classpath:/applicationContext*.xml
	    </param-value>
	 </context-param>
</web-app>

 3)添加applicationContext.xml文件,放在class下。

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:aop="http://www.springframework.org/schema/aop"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  

	<!--action-->
	<bean id="loginAction" scope="prototype" class="action.LoginAction"></bean>
	<!-- sessionfactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
		    <value>class:/hibernate.cfg.xml</value>
		</property>
	</bean>
</beans>

 4)整合Spring与Struts。在Struts的lib目录中找到struts2-spring-plugin-*.jar,引入到工程中。 
5.配置Hibernate 

        1)解压缩 hibernate-distribution-*.zip 。导入hibernate-distribution-*GA/lib/required目录中的jar包。 

                hibernate3.jar                         核心类库 
                antlr-2.7.6.jar                          代码扫描器,用来翻译HQL语句 
                commons-collections-3.1.jar    Apache Commons包中的一个,包含了一些Apache开发的集合类, 

                                                                功能比java.util.*强大 
                dom4j-1.6.1.jar                        一个Java的XML API,类似于jdom,用来读写XML文件的 
                javassist-3.4.GA.jar                 Javassist 字节码解释器 
                jta-1.1.jar                                标准的JTA API。 
                slf4j-api-1.5.2.jar 
                slf4j-nop-1.5.2.jar 


        2)创建Hibernate配置文件。在WEB-INF/calsses目录下建立链接数据库的配置文件hibernate.cfg.xml。

(这里我用的数据库是postgresql,*注意:需要导入postgresql_JDBC30.jar。 ) 

  hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
	    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
	    <property name="hibernate.connection.password">admin</property>
	    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/OpengisDataBase</property>
	    <property name="hibernate.connection.username">postgres</property>
	    <!--SQL dialect 方言-->
	    <property name="hibernate.dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect</property>
	    <property name="hibernate.format_sql">true</property>
	    <property name="hibernate.search.autoregister_listeners">false</property>
	    <property name="hibernate.show_sql">true</property>
	    <property name="hibernate.connection.pool_size">20</property>
	    <property name="hibernate.proxool.pool_alias">pool1</property>
	    <property name="hibernate.max_fetch_depth">1</property>
	    <property name="hibernate.jdbc.batch_versioned_data">true</property>
	    <property name="hibernate.jdbc.use_streams_for_binary">true</property>
	    <property name="hibernate.cache.region_prefix">hibernate.test</property>
	    <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
		<!--添加实体类映射文件-->
		<mapping resource="Login.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

   注意: 单独使用Hibernate需要创建Session工厂类HibernateSessionFactory.java 
                     (如果用Spring整合就不需要了。Spring会在applicationContext.xml中创建。) 
                      Hibernat 对数据库的操作是通过Session来实现的,这里的session不同于页面间传递参数的session, 
                      而是类似于JDBC中的 Connection。Session是Hibernate运作的中心, 
                      对象的生命周期、事务的管理、数据库的存取都与session息息相关。 
                      而Session是由HibernateSessionFactory创建的,是线程安全的, 
                      可以让多个执行线程同时存取HibernateSessionFactory而不会有数据共享的问题, 
                      但不能让多个线程共享一个Session。 

       3)Login.hbm.xml文件

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE hibernate-mapping PUBLIC  
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  
<hibernate-mapping package="包名">  
    <class name="类名" table="表名">  
        <id name="主键在java类中的字段名" column="对应表中字段" type="类型 ">  
            <generator class="主键生成策略"/>  
        </id>  
    </class>  
</hibernate-mapping>  

  6.Spring整合Hibernate。Spring对hibernate的Session的创建、提交、关闭的整个生命周期进行管理。 
        1)  配置sessionFactory,让spring来创建Session。在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"  
        xmlns:aop="http://www.springframework.org/schema/aop"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  

	<!--action-->
	<bean id="loginAction" scope="prototype" class="action.LoginAction"></bean>
	<!-- sessionfactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
		    <value>class:/hibernate.cfg.xml</value>
		</property>
	</bean>
</beans>




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值