基于MyEclipse8.5注解方式整合SSH框架

看过了很多整合的文章,但是自己做的时候总会出现这样那样的问题,在经历过几次失败尝试后现将自己整合过程整理如下:

首先作以下说明:

1、此次整合中,添加Spring和Hibernate支持是通过MyEclipse自身添加的;

2、整合之前需要准备以下包:

     其中spring包:antlr.jar、asm-2.2.2.jar、asm-commons-2.2.2.jar、cglib-nodep-2.1_3.jar

            struts包:commons-beanutils-1.8.0.jar、commons-collections.jar、commons-fileupload.jar、commons-io.jar、commons-lang-2.4.jar、commons-logging.jar、ognl.jar、struts2-convention-plugin-2.1.8.jar、struts2-core.jar、struts2-spring-plugin-2.1.8.jar、struts-getterAndSetter-plugin.jar、xwork-core.jar

           otherJAR包:log4j.jar、freemarker.jar

3、主要配置文件,分别是:工程启动时加载的文件web.xml,Spring的配置文件applicationContext.xml,struts配置文件struts.xml,其它文件:dataSource.properties(数据库加载配置,以防与其它配置文件混淆)、log4j.properties



整合过程如下:

1、 新建一个Web Project.

2、 添加Spring支持


3、 记住选择6个核心JAR包:

Spring 3.0 AOP Libraries

Spring 3.0 Core Libraries

Spring 3.0 PersistenceCore Libraries

Spring 3.0 PersistenceJDBC Libraries

Spring 3.0 J2EE Libraries

Spring 3.0 Web Libraries

如下图所示选择:


至此Spring支持已添加完成。

4、 添加Hibernate支持





此处添加数据源不再赘述



此时需要注意的是,Hibernate支持虽已添加,但是此时存在Spring 和Hibernate 包冲突,应删除antlr-2.7.6.jar包,然后添加Spring包:antlr.jar

                            asm-2.2.2.jar

                            asm-commons-2.2.2.jar

                            cglib-nodep-2.1_3.jar

 

5、 手动添加Struts支持(也就是添加几个JAR包)

所需JAR包如下:


增加struts.xml配置文件

<?xml version="1.0"encoding="UTF-8"?>

 

<!DOCTYPE struts PUBLIC

       "-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"

       "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

    <package name="default"extends="struts-default,getterAndSetter">

        <result-types>

            <result-type name="json" class="tx.web.struts2.plugins.JsonResultType"/>

            <result-type name="ajaxdata" class="tx.web.struts2.plugins.AjaxDataResultType"/>

            <result-type name="string" class="tx.web.struts2.plugins.StringResultType"/>

        </result-types>

 

        <global-results>

            <result name="login" type="redirect">/main/login</result>

        </global-results>

    </package>

</struts>

1、 添加Log4j和freemarker支持

加入log4j.jar和freemarker.jar包即可

7、此时查看自己的Web Project里JAR包里有没有重复的JAR包

删除重复的(commons-collections-3.1.jar、log4j-1.2.14.jar)

8、 更改配置文件,主要是web.xml 、applicationContext.xml

Web.xml文件如下:

<?xml version="1.0"encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    version="2.5">

    <!--

       Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔此参数用于后面的Spring

       Context Loader

    -->

 

    <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

    <!-- 配置Spring容器,指定Spring文件的路径,让Spring知道事务管理的bean所在 -->

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>/WEB-INF/applicationContext.xml</param-value>

    </context-param>

 

    <context-param>

       <param-name>log4jConfigLocation</param-name>

       <param-value>/WEB-INF/log4j.properties</param-value>

    </context-param>

   

    <!-- Character Encoding filter

           启动CharacterEncodingFilter监听器,用来设置编码格式

    -->

    <filter>

       <filter-name>encodingFilter</filter-name>

       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

       <init-param>

           <param-name>encoding</param-name>

           <!-- 设置为utf-8 -->

           <param-value>UTF-8</param-value>

       </init-param>

       <init-param>

           <param-name>forceEncoding</param-name>

           <param-value>true</param-value>

       </init-param>

    </filter>

 

    <filter-mapping>

    <!-- 设置过滤的路径-->

       <filter-name>encodingFilter</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

 

 

    <!--

       添加事务配置Hibernate使用Session的关闭与开启由Spring来管理,针对Hibernate懒加载,把一个Hibernate

       Session和一次完整的请求过程对应的线程相绑定

    -->

    <!-- Hibernate Open Session InView filter -->

    <filter>

       <filter-name>hibernateOpenSessionInViewFilter</filter-name>

        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

       <init-param>

       <!-- 指定在spring中配置的SessionFactory的bean id,

                     默认值为:sessionFactory -->

           <param-name>sessionFactoryBeanName</param-name>

           <param-value>sessionFactory</param-value>

       </init-param>

    </filter>

 

    <filter-mapping>

       <filter-name>hibernateOpenSessionInViewFilter</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

   

   

    <filter>

       <filter-name>struts2Filter</filter-name>

        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

    </filter>

    <filter-mapping>

       <filter-name>struts2Filter</filter-name>

       <url-pattern>/main/*</url-pattern>

    </filter-mapping>

 

    <!--

    Spring ApplicationContext 载入

    web启动后,用来启动spring的监听器,它读取参数contextConfigLocation,用 来初始化spring

    -->

    <listener>

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

    </listener>

 

    <!-- Spring 刷新Introspector防止内存泄露 -->

    <listener>

        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

    </listener>

    <!-- 定义LOG4J监听器 -->

    <listener>

       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

    </listener>

</web-app>


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:jee="http://www.springframework.org/schema/jee"

    xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"

    default-lazy-init="true">

 

    <!-- 使用context:component-scan

            让Bean定义注解工作起来 

            且

            隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor-->

    <!--Spring支持以下4种类型的过滤方式   此种是通过正则表达式过滤一些类  -->

    <!--base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理-->

    <context:component-scan base-package="service,dao,entity">

       <context:include-filter type="regex"

           expression="service\.*" />

       <context:include-filter type="regex"

           expression="dao\.*" />

           <context:include-filter type="regex"

           expression="entity\.*" />

 

    </context:component-scan>

 

    <bean id="configBean"

       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

       <property name="location"value="/WEB-INF/dataSource.properties" />

    </bean>

 

    <bean id="dataSource"

       class="org.springframework.jdbc.datasource.DriverManagerDataSource">

       <property name="driverClassName"value="${dataSource.driverClassName}" />

       <property name="url"value="${dataSource.url}" />

       <property name="username"value="${dataSource.username}" />

       <property name="password"value="${dataSource.password}" />

    </bean>

 

    <bean id="sessionFactory"

       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

       <!-- 指定Hibernate的数据源,即dataSource -->

       <property name="dataSource"ref="dataSource" />

       <!-- 定义Hibernate的SessionFactory的属性 -->

       <property name="hibernateProperties">

           <props>

               <!--指定数据库方言  -->

              <prop key="hibernate.dialect">${hibernate.dialect}</prop>

              <!-- 显示hibernate持久化操作所生成的SQL -->

              <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>

              <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>

           </props>

       </property>

     <!-- 配置实体类 ,即所需要hibernate持久化的类所在的包-->

       <property name="packagesToScan">

           <list>

              <value>entity</value>

           </list>

       </property>

    </bean>

 

    <!-- 声明一个Hibernate事务管理器 -->

    <bean id="transactionManager"

       class="org.springframework.orm.hibernate3.HibernateTransactionManager">

       <property name="sessionFactory"ref="sessionFactory" />

    </bean>

    <!-- 配置事务的开启可以基于注解的形式 ,即可使用@Transactional 注解-->

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>


一般情况下会在WEB-INF目录下新建一dataSource.properties,把数据库加载连接语句同配置文件分离开来。现贴出我的dataSource.properties

dataSource.driverClassName=oracle.jdbc.driver.OracleDriver

dataSource.url=jdbc:oracle:thin:@192.168.18.107:1521:test

dataSource.username=test

dataSource.password=test

hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

hibernate.show_sql=true

hibernate.format_sql=true


9、 在WEB-INF目录下添加Log4j配置文件log4j.properties

# Configure logging fortesting: optionally with log file

log4j.rootLogger=WARN, stdout

# log4j.rootLogger=WARN, stdout,logfile

 

<!--Appender ???????? org.apache.log4j.ConsoleAppender?????-->

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

<!--Layout???????  org.apache.log4j.PatternLayout???????????-->

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

<!--???? -->

<!--%d ???????????????????ISO8601???????????????%d{yyy MMMddHH:mm:ss , SSS}-->

<!--%p ???????DEBUG?INFO?WARN?ERROR?FATAL -->

<!--%c ?????????????????? -->

<!--%m ??????????-->

<!--%n ??????????Windows????\r\n??Unix????\n?-->

log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

 

log4j.appender.logfile=org.apache.log4j.FileAppender

log4j.appender.logfile.File=target/applicationContext.log

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

至此,SSH框架搭建完成。



集成freemarker插件,freemarker.properties

template_update_delay=1
datetime_format=yyyy-MM-dd HH:mm:ss
date_format=yyyy-MM-dd
time_format=HH:mm:ss
number_format=0.######;
boolean_format=true,false
auto_import="/WEB-INF/content/layout/base.ftl" as p
whitespace_stripping=true
default_encoding=UTF-8
tag_syntax=auto_detect
url_escaping_charset=UTF-8


测试见下篇







  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值