读书人

struts2+spring2.0+hibernate3.2调整

发布时间: 2012-11-09 10:18:48 作者: rapoo

struts2+spring2.0+hibernate3.2整合

????一)整合工具:myeclipse8.5.

??? 二)首先导入hibernate jar包。把包需要拷贝到WEB-INF/LIB目录下。导入spring jar包,不需要导入全部jar包,利用myeclipse导入前4个jar包,以及spring web的那个jar包即可。 导入struts2 jar包,需要commons-logging-1.0.4.jar、ognl-2.6.11.jar、xwork-2.0.7.jar、struts2-spring-plugin-2.0.14.jar、spring-core-2.0.5.jar,freemarker-2.3.8.jar这6个jar包,但是我们在导入spring jar包的时候已经包括了ommons-logging-1.0.4.jar,因此这个jar包可以去掉不要。记得添加自己用到的数据库的jar包。

??? 三)环境准备好之后,查看jar包,会发现asm的这个jar包有两个去掉其中一个,留下asm.jar,因为spring依赖的那个asm和hibernate依赖的asm会发生冲突。

??? 四)配置web.xm文件。配置如下:

???

<?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">  <filter>     <filter-name>struts2</filter-name>     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  </filter>   <filter-mapping>     <filter-name>struts2</filter-name>     <url-pattern>/*</url-pattern>  </filter-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <listener>    <listener-class>      org.springframework.web.context.ContextLoaderListener    </listener-class>  </listener></web-app>

?配置applicationContext.xml 配置如下:

?

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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.0.xsd"> <bean id="dataSource" value="net.sourceforge.jtds.jdbc.Driver"></property>     <property name="url" value="jdbc:jtds:sqlserver://localhost:1433//mytest"></property>     <property name="username" value="sa"></property>     <property name="password" value="sa"></property>     <property name="maxWait" value="200"></property>     <property name="maxActive" value="100"></property>     <property name="maxIdle" value="30"></property>     <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="sessionFactory" ref="userService"></property> </bean></beans>

?

?配置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 name="struts2" extends="struts-default">          <action name="login" class="userAction">         <result name="success">/success.jsp</result>         <result name="error">/index.jsp</result>     </action>   </package></struts>

?

这里要特别注意的是,配置struts.xml文件的时候,action的class不能写类的全路径了。要写出你在applicationContext.xm文件配置这个类的ID。 如果写了类的全路径,那么spring不会帮你注入了。我就因为这个问题纠结了1个多小时。下面是我上传的一个简单的登录的实例。

读书人网 >软件架构设计

热点推荐