自己整理的SSH(DWR)四大框架整合
整合SSH三大框架:先添加Spring支持,再添加Hibernate支持,最后添加Struts2支持。
按照此文档整合完成之后,将实现一个简单的登录功能。
使用MyEclipse开发工具整合步骤:
第一步:创建Web项目SSH2
图1:创建Web项目SSH2
第二步:创建数据库驱动(连接数据库)
图2:创建数据库驱动
第三步:添加Spring支持
图3:添加Spring支持
3.1所需要添加的JAR包
图4:Spring所需要的JAR包-1
图5:Spring所需要的JAR包-2
第四步:添加Hibernate支持
图6:添加Hibernate支持
4.1:选择配置文件(1.独立的2.与Spring同用一个)
图7:选择配置文件
4.1.1选择独立的配置文件(hibernate.cfg.xml)
图8:选中使用Hibernate独立配置文件“hibernate.cfg.xml”
图9:创建Hibernate独立配置文件“hibernate.cfg.xml”
4.1.2:选择与Spring同用一个配置文件
图10:选中与Hibernate同用一个配置文件
图11:在Spring配置文件“applicationContext.xml”中创建Hibernate配置信息
4.1.3选择是否要创建SessionFactory
由于我们使用了Spring框架,它会帮我们常见SessionFactory,所以我们选择不创建。
图12:创建SessionFactory
4.2:选择数据库驱动
图13:选择数据库驱动
选中此按钮,则会由项目实体配置文件中的配置信息,动态在数据库中创建数据库表。
图:14:动态创建数据库表
第五步:添加Struts2支持
图15:添加Struts2支持
图16:Struts2需要添加的JAR包
第六步:支持整合完毕,项目结构
图17:支持添加完后的项目结构
第七步:在web.xml中配置相关信息
7.1没有手动配置其他信息的web.xml信息
图18:没有手动配置其他信息的web.xml信息
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>*.action</url-pattern> </filter-mapping> </web-app> |
web.xml
7.2添加其他配置的web.xml信息
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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"> <!-- 下面两个配置需要放在此配置文件的最上面 --> <!-- 配置applicationContext.xml文件的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <!-- 使用监听器对spring进行自动实例化 ,放在application范围内 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 使用servl类对spring进行自动实例化 ,放在application范围内 --> <!-- <servlet>--> <!-- <servlet-name>context</servlet-name>--> <!-- <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> --> <!-- <load-on-startup>1</load-on-startup>--> <!-- </servlet>--> <!-- 设置访问项目时的首页 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Struts2过滤器 --> <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>*.action</url-pattern> </filter-mapping> <!-- 配置过滤器(可用于设置程序编码) --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>com.fxq.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置404错误信息显示的页面 --> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> </web-app> |
第八步:在applicationContext.xml中配置相关信息
8.1没有手动配置其他信息的applicationContext.xml
图19:没有手动配置其他信息的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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> </beans> |
applicationContext.xml
8.2配置过其他信息的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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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"> <!-- 配置事务管理(需要更改此配置文件的头部) --> <bean id="tx" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 声明采用注解方式管理事务(在需要事务的DAO层中的方法的上面加上@Transactional) --> <tx:annotation-driven transaction-manager="tx" />
<!-- 配置自动管理事务 开始 --> <!-- 事务通知操作,使用的事务管理器引用自transactionManager --> <!-- <tx:advice id="txAdvice" transaction-manager="tx">--> <!-- <tx:attributes>--> <!-- 指定哪些方法需要加入事务 --> <!-- <tx:method name="save*" propagation="REQUIRED" />--> <!-- <tx:method name="delete*" propagation="REQUIRED" />--> <!-- <tx:method name="update*" propagation="REQUIRED" />--> <!-- </tx:attributes>--> <!-- </tx:advice>--> <!-- 如果不进行该配置,则需要在需要事务的方法上面添加:@Transactional,进行手动添加 --> <!-- <aop:config>--> <!-- 切入点指明了在执行com.fxq.dao包中的所有方法时产生事务拦截操作 --> <!-- <aop:pointcut id="daoMethods" expression="execution(* com.fxq.dao.*.*(..))" /> --> <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice --> <!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />--> <!-- </aop:config>--> <!-- 配置自动管理事务 结束 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> </beans> |
applicationContext.xml
第九步:由数据库生成对应的表实体和实体映射文件
图20:由数据库生成实体及映射文件
图21:先择实体中主键的处理方式
第十步:配置strut.xml文件中的相关信息
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" extends="struts-default"> <action name="loginAction" class="com.fxq.action.LoginAction"> <result name="index">/index.jsp</result> </action> </package> </struts> |
第十一步:OK,整合完成!实现登录功能。
第十二布:加入DWR框架
12.1添加DWR的JAR包dwr.jar
12.2添加需要的两个js文件
12.3创建配置文件dwr.xml,用于配置DWR的相关信息
<?xml version="1.0" encoding="UTF-8"?> <dwr> <allow> <!-- 使用spring依赖注入 --> <create creator="spring" javascript="logins"> <param name="beanName" value="loginDWR"></param> </create> <convert match="com.fxq.entity.Users" converter="bean"> <!-- 限制可用的属性 --> <param name="include" value="id,name,password"></param> </convert> </allow> </dwr> |
12.4在web.xml中配置DWR
<!-- 配置DWR的相关信息 --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <!-- 显示测试页面 --> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> |
12.5测试要使用的方法
测试路径:http://localhost/SSH2/dwr/ |
12.6在页面中书写js代码
<script type='text/javascript' src='/SSH2/dwr/interface/logins.js'> </script> <script type='text/javascript' src='/SSH2/dwr/engine.js'> </script> <script type="text/javascript"> //单击"检验用户名"事件 function doValidate() { var name = document.getElementById("uname"); //使用DWR,调用业务逻辑类方法 logins.validateName(name.value, callback); } //回调函数 function callback(data) { alert(data); } </script> |
第十三步:OK,大功告成!
注:没有的界面全部点“下一步”
注意需要将库中的sam.2.2.3.jar删除,否则它和sam.jar包冲突