struts1.x和spring整合

java.lang.NullPointerException
	at com.sdb.action.ProductAction.execute(ProductAction.java:34)
	at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
	at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
	at com.sdb.ABFilter.doFilter(ABFilter.java:21)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:843)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:679)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1303)
	at java.lang.Thread.run(Thread.java:595)



struts一定要给spring托管,不然action里就没法取得service bean,报nullPointerException 如上。在要struts-config.xml中加入如下

<controller>
		<set-property property="processorClass"
			value="org.springframework.web.struts.DelegatingRequestProcessor" />
	</controller>


一、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> 






你可能感兴趣的:(struts1.x和spring整合)