一、配置Struts2:
1、新建一个WEB工程,加入Struts2的五个核心jar包:
- freemarker-2.3.15.jar
- commons-fileupload-1.2.1.jar
- ognl-2.7.3.jar
- struts2-core-2.1.8.jar
- xwork-core-2.1.6.jar
2、在web.xml里面为Struts2添加支持:
<?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">
<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>/*</url-pattern>
</filter-mapping>
</web-app>
这个filter-class还可以写成
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
3、添加struts.xml文件:
在classpath下添加struts.xml文件,只需要一个空壳子就能让Struts跑起来,constant和package等配置可以根据实际情况慢慢添加。
<?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>
</struts>
很多人喜欢把配置文件直接放在src目录下,反正我个人看着很不爽,所以一般我会新建一个resources目录,然后把配置文件都放进去,然后再eclipse里面把resources目录加进build path/Source里面就行了。另外,现在用Eclipse Indigo的Java EE视图,这哥们在导航栏里把全部jar都放出来了,拖动起来很是麻烦,我找了个办法,就是自己建一个User Library,让它引用到工程全部的jar包,然后让工程引用到这个User Library而不是直接引用到全部的jar包,这样导航栏就清爽多了!附上一个博客地址,上面有关于Struts2的一些基础知识:
http://www.cnblogs.com/suxiaolei/
二、配置Spring:
1、依旧在web.xml里为Spring添加支持,所以web.xml就成了这样:
<?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">
<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>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>insert.jsp</welcome-file>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、添加两个jar包,分别是:
- spring.jar
- commons-logging.jar
3、添加配置文件:
又是配置文件?当然还是放在resources里头。上面的的Spring配置使用了*通配符,所以以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"
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">
</beans>
头好像多了点,因为加入了各种的支持。
4、Spring整合Struts:
现在可以把Spring配置到Struts里头了,先在struts.xml里面添加配置常量
<constant name="struts.objectFactory" value="spring"/>
然后还要加一个jar包:struts2-spring-plugin-2.0.11.1.jar,这样就OK了!
三、配置iBatis:
在Spring的配置文件里面添加如下代码:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>sa</value>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:SqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
添加iBatis的配置文件SqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true"
maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />
</sqlMapConfig>
当然还要添加ibatis-2.3.3.720.jar,这样就整合了Spring和iBatis,获取SqlMapClient只需要extends SqlMapClientDaoSupport,然后调用getSqlMapClientTemplate()就可以对数据库进行操作了。