Struts1.3,Spring2.5,JDBC整合

1.导入Struts1.3,Spring2.5,JDBC的jar文件。

  • Struts1.3的jar文件在struts-blank示例中复制即可;
  • Spring2.5的jar文件dist目录下的spring.jar以及modules目录下的大部分jar;
  • JDBC的jar文件即JDBC驱动程序。

2.在web.xml中定义Struts1.3的ActionServlet,并映射到*.do。

View Code
 1   <!-- Standard Action Servlet Configuration -->
2 <servlet>
3 <servlet-name>action</servlet-name>
4 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
5 <init-param>
6 <param-name>config</param-name>
7 <param-value>/WEB-INF/struts-config.xml</param-value>
8 </init-param>
9 <load-on-startup>2</load-on-startup>
10 </servlet>
11
12 <!-- Standard Action Servlet Mapping -->
13 <servlet-mapping>
14 <servlet-name>action</servlet-name>
15 <url-pattern>*.do</url-pattern>
16 </servlet-mapping>

 

  小技巧:在struts-blank示例中复制web.xml中的内容。

3.在web.xml中配置Spring2.5的上下文参数,并声明监听器。

View Code
 1   <context-param>
2
3 <param-name>contextConfigLocation</param-name>
4 <param-value>classpath:applicationContext.xml</param-value>
5
6 </context-param>
7
8 <listener>
9 <listener-class>
10 org.springframework.web.context.ContextLoaderListener
11 </listener-class>
12 </listener>

 

4.在struts-config中配置控制器,将action交给Spring掌管。

View Code
1 <controller>
2 <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
3 </controller>

 

5.在applicationContext.xml中配置dataSource。

View Code
1     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
2 <property name="driverClassName" value="com.mysql.jdbc.Driver" />
3 <property name="url" value="jdbc:mysql://localhost:3306/dbName" />
4 <property name="username" value="root" />
5 <property name="password" value="" />
6 </bean>

 

基本配置就是这些。还有一些注意事项:

  • formBean仍然在struts-config.xml中配置;
  • action仍然需要在struts-config.xml中配置,但是可以省略type属性(因为真正用的是Spring中定义的);
  • applicationContext.xml中定义的action的name(只能用name不能用id,因为是"/"开头)要跟struts-config.xml中定义的action的path完全一致;
  • 有了dataSource就可以使用Spring的jdbcTemplate类来简化JDBC编程。
posted @ 2012-03-18 13:10  我愿为蛹  阅读(402)  评论(0)    收藏  举报