fayeyiwang 2009-12-12
一、struts和spring整合:
1、让struts的action继承spring的ActionSupport:
ApplicationContextcontext=getWebApplicationContext();
CourseServicecourseService=context.getBean("courseService");
2、配置方式
修改struts的配置文件
<actionpath="/listCourses"type="org.springframework.web.struts.DelegatingActionProxy"/>
使用spring的DI为struts的action注入所需对象
<beanname="/listCourses"class="com.springinaction.training.struts.ListCourseAction">
<propertyname="courseService">
<refbean="courseService"/>
</property>
</bean>
3、替换掉struts的RequestProcessor:
<controllerprocessorclass="org.springframework.web.struts.DelegatingRequestProcessor"/>
此时struts的配置就无需做什么改变:
<actionpath="/listCourses"type="com.springinaction.training.struts.ListCourseAction"/>
或者:
<actionpath="/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-inclassname="org.springframework.web.struts.ContextLoaderPlugIn">
<set-propertyproperty="contextConfigLocation"value="/WEB-INF/training-servlet.xml,/WEB-INF/…"/>
</plug-in>
三、spring处理配置DAO
1、GettingaDataSourcefromJNDI:
<beanid="dataSource"class="org.springframework.jndi.JndiObjectFactoryBean">
<propertyname="jndiName">
<value>java:comp/env/jdbc/myDatasource</value>
</property>
</bean>
2、CreatingaDataSourceconnectionpool:
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driver">
<value>${db.driver}</value>
</property>
<propertyname="url">
<value>${db.url}</value>
</property>
<propertyname="username">
<value>${db.username}</value>
</property>
<propertyname="password">
<value>${db.password}</value>
</property>
</bean>
配置属性:
<beanid="datasourcecfg"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="location">
<value>classpath:datasource.properties</value>
</property>
</bean>
3、spring对jdbc的支持:
publicvoidinsertPerson(Personperson)throwsSQLException{
Connectionconn=null;
PreparedStatementstmt=null;
try{
conn=dataSource.getConnection();
stmt=conn.prepareStatement("insertintoperson("+
"id,firstName,lastName)values(?,?,?)");
stmt.setInt(0,person.getId().intValue());
stmt.setString(1,person.getFirstName());
stmt.setString(2,person.getLastName());
stmt.executeUpdate();
}catch(SQLExceptione){
LOGGER.error(e);
}finally{
try{if(stmt!=null)stmt.close();}catch(SQLExceptione){LOGGER.warn(e);}
try{if(conn!=null)conn.close();}catch(SQLExceptione){LOGGER.warn(e);}
}
}
publicintinsertPerson(Personperson){
Stringsql="insertintoperson(id,firstName,lastName)values(?,?,?)";
Object[]params=newObject[]{person.getId(),person.getFirstName(),person.getLastName()};
int[]types=newint[]{Types.INTEGER,Types.VARCHAR,Types.VARCHAR};
returnjdbcTemplate.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>
六、对于数据库增删改过种中防止重复和不能删除项的处理意见: