一、导入struts2支持
1、导入struts2所需要的包:
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
2、web.xml配置:
<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>
3、src下struts.xml内容:
<!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.i18n.encoding" value="GBK" />
<!-- struts2委托spring管理 -->
<constant name="objectFactory" value="spring" />
<package name="tutorial" extends="struts-default">
<action name="getHw" class="HellowWord" //此处的class用spring
代理时需要用spring的<bean Id="此名称">
method="getHw">
<result name="success">index.jsp</result>
</action>
<action name="getHwByXML" class="HellowWord"
method="getHwByXML">
<result name="success">xml.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>
二、导入spring支持
1、导入spring所需要的包
spring.jar
2、创建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="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 数据库德配置 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="${jdbc.driverClassName}">
</property>
<property name="url" value="${jdbc.url.ds1}"></property>
<property name="username" value="${jdbc.uid}"></property>
<property name="password" value="${jdbc.pwd}"></property>
</bean>
<!-- 读取ibatis配置文件 -->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:sql-map-config.xml</value>
</property>
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<!-- 事务的定义 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 实现层的注入 -->
<bean id="TScheduleDaopx"
class="com.cn.zy.dao.impl.TScheduleDaoImpl">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- 代理类的配置,将实现层与实务层剥离 ,采用spring的AOP技术实现,只需
配置就好,无需新建类-->
<bean id="userDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean
">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref bean="TScheduleDaopx" />
</property>
<property name="transactionAttributes">
<props>
<prop
key="insert*">PROPAGATION_REQUIRED</prop>
<prop
key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- 服务层的注入 -->
<bean id="TScheduleBizImplpx"
class="com.cn.TScheduleBizImpl">
<property name="dao">
<ref bean="TScheduleDaopx" />
</property>
</bean>
<!-- action方法的注入 -->
<bean id="HellowWord" class="com.cn.HellowWord">
<property name="tbiz">
<ref bean="TScheduleBizImplpx" />
</property>
</bean>
</beans>
3、创建jdbc.properties
内容:
jdbc.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
jdbc.url.ds1=jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=TrainDB;Se
lectMethod=cursor
jdbc.uid =sa
jdbc.pwd=sa
三、导入ibatis支持
1、导入包:
commons-codec-1.3.jar
commons-collections.jar
commons-dbcp.jar
commons-httpclient-3.0.jar
commons-lang-2.2.jar
commons-logging-1.0.4.jar
commons-pool.jar
ibatis-2.3.4.726.jar
struts2-spring-plugin-2.0.11.1.jar //struts2使用spring代理时需要使用此插件
2、创建实体类配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<!-- 自动参数映射 -->
<select id="getObjectByCode" resultClass="com.cn.zy.model.TSchedule"
parameterClass="java.lang.String">
SELECT * FROM T_schedule WHERE schedulecode=#schedulecode#
</select>
<select id="getAll" resultClass="com.cn.model.TSchedule">
SELECT * FROM T_schedule WHERE 1=1
</select>
<select id="getAllUserInfo"
resultClass="com.cn.model.TSchedule">
SELECT * FROM T_schedule WHERE 1=1
</select>
</sqlMap>
3、创建sql-map-config.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>
<sqlMap resource="com/cn/zy/dao/sqlmap/map/TSchedule.xml" />
<sqlMap resource="com/cn/zy/dao/sqlmap/map/TScheduleDetial.xml" />
<sqlMap resource="com/cn/zy/dao/sqlmap/map/RecordPaging.xml" />
</sqlMapConfig>
在spring的applicationContext.xml中引用此文件
4、javabean调用
@SuppressWarnings("unchecked")
public List<TSchedule> getAll() {
// TSchedule item
return getSqlMapClientTemplate().queryForList
("getAllUserInfo");
}
// 授权设备给用户
public List<TSchedule> getObjectByCode(String code) {
Map map = new HashMap();
map.put("cid", new Integer(code));
List list = null;
try {
list = getSqlMapClientTemplate().queryForList
("getDetailById", map);
} catch (Exception e) {
// throw new DaoException("DAOException: " + e);
e.printStackTrace();
}
return list;
}
四、导入aop支持
aopalliance.jar
asm-2.2.3.jar
asm-commons-2.2.3.jar
asm-util-2.2.3.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
五、最后导入日志、jstl
log4j-1.2.11.jar
jstl-1.2.jar