[size=small] [color=darkblue]1.新建一个项目BookManage,在src目录下面分别建package。如com.dwj.action,com.dwj.javabean,com.dwj.dao,com.dwj.util,com.dwj.test,这些包中分别存放相应的java文件。[/color] [color=darkblue]
2.导入hibernate 3.20,生成hibernate.cfg.xml文件,该文件路径是在src目录下,hibernate.cfg.xml中对应的数据源是自己建立的对应的数据库。
3.在com.dwj.javabean下建Book对象的javabean,book.hbm.xml,ExportToDB.java文件生成库表book。或者利用sql语句先建立库表book,再利用myeclipse的逆向工程自动生成javabean。
4.导入spring包,自动生成applicationContext.xml文件,该文件也在src目录下面:
[/color]<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
</beans>
[color=darkblue]在项目的WebRoot/WEB-INF/lib文件夹下放入struts2-spring-plugin-2.x.x.x.jar包。
5.导入struts2包,至少需要5个包:struts2-core-2.x.x.jar,xwork-2.x.x.jar ,ognl-2.6.x.jar ,freemarker-2.3.x.jar ,commons-logging-1.1.x.jar 。其他的包有classes12.jar,standard.jar,jstl.jar。把这些包放在项目的WebRoot/WEB-INF/lib文件夹下面。
6.配置web.xml:
[/color]<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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>
</web-app>
[color=darkblue] 这里要配置spring监听器<listener>以及<context-param>,<context-param>中默认的是把applicationContext.xml放在/WEB-INF/目录下的,所以应该把之前配置在src下的applicationContext.xml复制到WebRoot/WEB-INF下,如果不改变src下的applicationContext.xml,则<context-param>改为:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
这是因为项目中src下的文件部署到tomcat服务器之后,是在>/WEB-INF/classes文件夹下。
7.src目录下面建立struts.xml文件:
[/color]<?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="GBK" />
[color=red]<!-- struts的注入交给spring去控制 --> [/color]
<constant name="struts.objectFactory" value="spring"/>
<include file="struts-default.xml"/>
[color=red] <!-- Add packages here -->[/color]
<package name="struts" extends="struts-default">
<action name="listUser" class="listAction">
<result name="success">/show2.jsp</result>
</action>
</package>
</struts>[/size]
2.导入hibernate 3.20,生成hibernate.cfg.xml文件,该文件路径是在src目录下,hibernate.cfg.xml中对应的数据源是自己建立的对应的数据库。
3.在com.dwj.javabean下建Book对象的javabean,book.hbm.xml,ExportToDB.java文件生成库表book。或者利用sql语句先建立库表book,再利用myeclipse的逆向工程自动生成javabean。
4.导入spring包,自动生成applicationContext.xml文件,该文件也在src目录下面:
[/color]<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
</beans>
[color=darkblue]在项目的WebRoot/WEB-INF/lib文件夹下放入struts2-spring-plugin-2.x.x.x.jar包。
5.导入struts2包,至少需要5个包:struts2-core-2.x.x.jar,xwork-2.x.x.jar ,ognl-2.6.x.jar ,freemarker-2.3.x.jar ,commons-logging-1.1.x.jar 。其他的包有classes12.jar,standard.jar,jstl.jar。把这些包放在项目的WebRoot/WEB-INF/lib文件夹下面。
6.配置web.xml:
[/color]<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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>
</web-app>
[color=darkblue] 这里要配置spring监听器<listener>以及<context-param>,<context-param>中默认的是把applicationContext.xml放在/WEB-INF/目录下的,所以应该把之前配置在src下的applicationContext.xml复制到WebRoot/WEB-INF下,如果不改变src下的applicationContext.xml,则<context-param>改为:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
这是因为项目中src下的文件部署到tomcat服务器之后,是在>/WEB-INF/classes文件夹下。
7.src目录下面建立struts.xml文件:
[/color]<?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="GBK" />
[color=red]<!-- struts的注入交给spring去控制 --> [/color]
<constant name="struts.objectFactory" value="spring"/>
<include file="struts-default.xml"/>
[color=red] <!-- Add packages here -->[/color]
<package name="struts" extends="struts-default">
<action name="listUser" class="listAction">
<result name="success">/show2.jsp</result>
</action>
</package>
</struts>[/size]