整合java框架struts,ibatis,spring可以作为java资料进行保留
步骤:
一.添加Spring 、Struts框架对web.xml文件的修改
1. 添加Spring框架
2. 在web.xml中引入Spring配置文件(注意:applicationContext.xml文件的路径)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/conf/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
3. 添加Struts框架
4. 在web.xml中引入Spring配置文件(注意:struts-config.xml文件的路径)
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/conf/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
5. web.xml自动添加的*.jsp文件起始路径(注意:jsp文件的访问路径)
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/emp.jsp</welcome-file>
</welcome-file-list>
二、对Spring配置文件applicationContext.xml文件的修改
1. 配置JDBC数据源驱动文件 jdbc.properties(服务器、端口、账户、密码因具体情况而定)
MySql:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=admin
Oracle:
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=scott
jdbc.password=tiger
MS Sql Server:
jdbc.driver= com.microsoft.jdbc.sqlserver.SQLServerDriver
jdbc.url= jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mssql
jdbc.username=sa
jdbc.password=sa
2. 将JDBC驱动源文件部署到ApplicationContext.xml配置文件中
<beanid="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="locations">
<list>
<value>/WEB-INF/conf/jdbc.properties</value>
</list>
</property>
</bean>
3. 将Data Source配置到ApplicationContext文件中
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<propertyname="driverClassName">
<value>${jdbc.driver}</value>
</property>
<propertyname="url">
<value>${jdbc.url}</value>
</property>
<propertyname="username">
<value>${jdbc.username}</value>
</property>
<propertyname="password">
<value>${jdbc.password}</value>
</property>
<propertyname="minIdle">
<value>1</value>
</property>
<property name="maxActive">
<value>25</value>
</property>
<property name="maxIdle">
<value>5</value>
</property>
</bean>
4. 在Spring配置中注入3中的datasource数据源
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource">
<reflocal="dataSource"/>
</property>
</bean>
5. 引入具体的sql语句配置文件(注意:sqlMapConfig.xml文件的路径)
<beanid="sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<propertyname="configLocation">
<value>/WEB-INF/conf/sqlMapConfig.xml</value>
</property>
<propertyname="dataSource">
<reflocal="dataSource"/>
</property>
</bean>
6. 注入操作数据库的dao(empDao是举例)
<beanid="empDao"class="com.web.ssi.emp.dao.EmpDaoImpl">
<propertyname="sqlMapClient">
<reflocal="sqlMapClient"/>
</property>
</bean>
三. 对Struts配置文件struts-config.xml文件的修改
1. 填写form-bean
<form-beans>
<form-beanname="empForm"type="com.web.ssi.emp.form.EmpForm"/>
</form-beans>
2. 填写action-mapping
<action-mappings>
<action
name="empForm"
path="/queryEmp"
type="org.springframework.web.struts.DelegatingActionProxy"
scope="request">
<forward name="queryEmp"path="/jsp/emp.jsp"/>
</action>
</action-mappings>
3. 填写message-resource 和plug-in(注意:action-servlet.xml文件路径)
<message-resourcesparameter="com.web.ssi.ApplicationResources"/>
<plug-inclassName="org.springframework.web.struts.ContextLoaderPlugIn">
<set-propertyproperty="contextConfigLocation"value="/conf/applicationContext.xml"/>
</plug-in>
4. Spring与Struts相对用的配置applicationContext.xml
<beans>
<beanname="/queryEmp"class="com.web.ssi.emp.action.EmpAction"singleton="false">
<property name="empService">
<refbean="empService"/>
</property>
</bean>
</beans>