struts2.0+spring2.5+hibernate3.3

我的开发工具是MyEclipse8.5 ,整合ssh的步骤一般是spring,hibernate,struts 。
spring和hibernate都是通过MyEclipse工具自动帮助生成的,而struts用的自己在官网下载的完整的struts2.0的包,
spring与hibernate的整合

在项目中通过菜单栏上的 MyEclipse->ProjectCapabilities->Add Spring Capabilites ,接着按照提示信息一步步往下面操作就可以了,
增加hibernate支持, MyEclipse->ProjectCapabilities->Add Hibernate Capabilites ,注意不要生成hibernate.cfg.xml文件,而是用applicationContext.xml文件替代了,下面是application.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-2.5.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/ssh"></property>
<property name="username" value="root"></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.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="connection.characterEncoding">utf8</prop>
</props>
</property>

<!-- 指定hibernate映射文件的位置 -->
<property name="mappingResources">
<value>com/southdigital/login/model/User.hbm.xml</value>
</property>
</bean>

<bean id="loginDao" class="com.southdigital.login.dao.impl.LoginDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<bean id="loginService" class="com.southdigital.login.service.impl.LoginServiceImpl">
<property name="loginDao">
<ref bean="loginDao"/>
</property>
</bean>

<bean name="loginAction" class="com.southdigital.login.struts2.action.LoginAction">
<property name="loginService">
<ref bean="loginService"/>
</property>
</bean>
</beans>

要写dao ,service,层。层层相互依赖,具体 .jsp->action->service->dao->database
按照上面的过程写代码,当然也可以倒过来,看自己的喜爱。


spring与struts的整合

一般导入Commons-loggin-1.0.4,ognl-2.6.11,struts-core-2.0.5,struts2-spring-plugin-2.0.14,xwork-2.0.7 这5个jia包拷贝到lib目录下。

struts.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="login" extends="struts-default">
<!-- 如果struts和spring整合,那么这里的class应该要与spring的name相同,而在spring中的class则指向真正的action类
在struts1中,是通过path与bean的name比配的
-->
<action name="loginAction" class="loginAction">
<result name="success">/success.jsp</result>
<result name="fail">/fail.jsp</result>
</action>
</package>
</struts>


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>

<!-- 指定spring配置文件的位置默认是在WEB-INF/applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 注册spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 处理页面传给后台的数据,进行utf-8点的编码 -->

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<!-- 处理避免session无效 -->

<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
<!-- struts2.1.9 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter -->
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>


整合最主要的是jar包,以及application.xml,struts.xml,web.xml这3个重要文件的内容

具体的代码可以参考 我的代码,这里lib下面的jar文件太大了,上传不了,所以就不复制到lib目录下面,自己可以去下载struts的jar,而spring,hibernate的jar可以用MyEclipse自带的jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值