struts1.x和spring整合

一、struts和spring整合:
1、让struts的action继承spring的ActionSupport:
ApplicationContext context = getWebApplicationContext();
CourseService courseService = context.getBean("courseService");
2、配置方式
修改struts的配置文件
<action path="/listCourses" type="org.springframework.web.struts.DelegatingActionProxy"/>
使用spring的DI为struts的action注入所需对象
<bean name="/listCourses" class="com.springinaction.training.struts.ListCourseAction">
<property name="courseService">
<ref bean="courseService"/>
</property>
</bean>
3、替换掉struts的RequestProcessor:
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
此时struts的配置就无需做什么改变:
<action path="/listCourses" type="com.springinaction.training.struts.ListCourseAction"/>
或者:
<action path="/listCourses"/>

二、spring的启动时机:
1、在web.xml中加入listener:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/training-service.xml,/WEB-INF/training-data.xml</param-value>
</context-param>
2、如果用的是struts,可以让struts启动时加载spring:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/training-servlet.xml,/WEB-INF/…"/>
</plug-in>
三、spring处理配置DAO
1、Getting a DataSource from JNDI:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/myDatasource</value>
</property>
</bean>
2、Creating a DataSource connection pool:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driver">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>

配置属性:
<bean id="datasourcecfg" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:datasource.properties</value>
</property>
</bean>

3、spring对jdbc的支持:
public void insertPerson(Person person) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = dataSource.getConnection();
stmt = conn.prepareStatement("insert into person (" +
"id, firstName, lastName) values (?, ?, ?)");
stmt.setInt(0, person.getId().intValue());
stmt.setString(1, person.getFirstName());
stmt.setString(2, person.getLastName());
stmt.executeUpdate();
}catch(SQLException e) {
LOGGER.error(e);
}finally {
try { if (stmt != null) stmt.close(); }catch(SQLException e) { LOGGER.warn(e); }
try { if (conn != null) conn.close(); }catch(SQLException e) { LOGGER.warn(e); }
}


public int insertPerson(Person person) {
String sql = "insert into person (id, firstName, lastName) values (?, ?, ?)";
Object[] params = new Object[] { person.getId(),person.getFirstName(),person.getLastName() };
int[] types = new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR };
return jdbcTemplate.update(sql, params, types);
}
4、spring对Hibernate的支持:

四、如何处理LazyInitializedException异常
<filter>
<filter-name>hibernateSessionFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateSessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

五、struts的分模块开发
<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/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>config/center</param-name>
<param-value>/WEB-INF/center-struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>config/student</param-name>
<param-value>
/WEB-INF/student-struts-config.xml
</param-value>
</init-param>
<init-param>
<param-name>config/admin</param-name>
<param-value>/WEB-INF/admin-struts-config.xml</param-value>
</init-param>
</servlet>

六、对于数据库增删改过种中防止重复和不能删除项的处理意见:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值