struts2+hibernate+spring的整合
写在文档之前:
在做项目时,不能太依靠开发工具,这样对以后的学习不利。这次在融合SSH框架时出现了不小的问题,耽搁了不少的时间。我觉得程序员应当对代码有一定在熟习度后再用框架。这样在以后的工作会事半功倍的。这次在做struts-2.1.2与spring2+hibernate3.1融合时由于包的冲突着实让人为难。花了不少的精力。
下面就简单的写一下整合过程
在myeclipse6.1下新建web工程 ssh。
添加spring支持。Spring包全选
添加hibernate支持
添加struts2支持
将struts-2.1.2-all.rar解压。
在struts-2.1.2\apps下有一个名为:struts2-blank-2.1.2.war文件。用winRAR解压后将其web-info/lib目录下的所有.jar包拷贝到新的工程中。同时还要将struts-2.1.2-all/lib下的struts2-spring-plugin-2.1.2.jar包持有拷贝到工程目录下。
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
</web-app>
Strutcs.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>
<constant name="struts.objectFactory" value="spring" />
<package name="default" extends="struts-default">
<action name="p" class="testAction" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
struts.properties 文件
objectFactory = spring
8、测试类
package test;
public class HelloWorld {
public HelloWorld()
{
System.out.println("构造函数");
}
public String execute(){
System.out.println("execute 方法里");
return "success";
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@192.168.1.119:1521:olap2">
</property>
<property name="username" value="olap"></property>
<property name="password" value="111111"></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.Oracle9Dialect
</prop>
</props>
</property>
</bean>
<bean id="testAction" class="test.HelloWorld"></bean>
</beans>
写在文档之前:
在做项目时,不能太依靠开发工具,这样对以后的学习不利。这次在融合SSH框架时出现了不小的问题,耽搁了不少的时间。我觉得程序员应当对代码有一定在熟习度后再用框架。这样在以后的工作会事半功倍的。这次在做struts-2.1.2与spring2+hibernate3.1融合时由于包的冲突着实让人为难。花了不少的精力。
下面就简单的写一下整合过程
在myeclipse6.1下新建web工程 ssh。
添加spring支持。Spring包全选
添加hibernate支持
添加struts2支持
将struts-2.1.2-all.rar解压。
在struts-2.1.2\apps下有一个名为:struts2-blank-2.1.2.war文件。用winRAR解压后将其web-info/lib目录下的所有.jar包拷贝到新的工程中。同时还要将struts-2.1.2-all/lib下的struts2-spring-plugin-2.1.2.jar包持有拷贝到工程目录下。
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
</web-app>
Strutcs.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>
<constant name="struts.objectFactory" value="spring" />
<package name="default" extends="struts-default">
<action name="p" class="testAction" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
struts.properties 文件
objectFactory = spring
8、测试类
package test;
public class HelloWorld {
public HelloWorld()
{
System.out.println("构造函数");
}
public String execute(){
System.out.println("execute 方法里");
return "success";
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@192.168.1.119:1521:olap2">
</property>
<property name="username" value="olap"></property>
<property name="password" value="111111"></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.Oracle9Dialect
</prop>
</props>
</property>
</bean>
<bean id="testAction" class="test.HelloWorld"></bean>
</beans>