项目需要,在原来Struts的基础上,配了Spring,数据库访问也用JDBC代替,由于Spring出色的控制反转,JDBC连接的工作基本上不需要考虑,只要专注于数据抽取就可以了,下边把代码贴一下,顺便说说一些小细节。
Spirng和Struts结合,主要有三种方式:
1,使用ActionSupport类
2,覆盖RequestProcessor
3,将Action委托给Spring
文章
<使用 Spring更好地处理Struts动作>中,对这三种方法都分析得比较详细,我在这里归纳一下.
使用第一种方法,是最简单的,不需要其他任何配置,只需要在把继承Action,改成继承ActionSupport,带来的问题就是Struts与Spring,紧耦合,以后不使用Spring配置时,需要修改代码.但其实,我目前觉得使用此方法,有一个好处是可以方便的得到WebApplicationContext对象,不然,就需要使用ClassPathXmlApplicaiton("...")来取得Context对象,不是很方便.其实,看DelegationActionUtils的源码,applicationContext对象也只是,这样子通过sturts的plugin取得的.
actionServlet.getServletContext().getAttribute(
ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + modulePrefix)
第二种方法,所有Action的分发都是通过ActionServlet的,而实际上的操作是由RequestProcessor 完成的,因此,可以把它覆盖,引入Spring
The RequestProcessor is where the majority of the core processing occurs for each request. Since version 1.3, the default Request Processor
(ComposableRequestProcessor)
is composed using
Jakarta Commons Chain, which is an implementation of the
Chain of Responsibility pattern (CoR).
The
<controller>
element allows you to configure the ActionServlet. Many of the controller parameters were previously defined by servlet initialization parameters in your
web.xml
file but have been moved to this section of
struts-config.xml
in order to allow different modules in the same web application to be configured differently.
第三种是最推荐的方法
顺便说一下,plugin在struts的概念,来自UserGuide:
The
PlugIn interface extends Action and so that applications can easily hook into the ActionServlet lifecycle. This interface defines two methods, init() and destroy(), which are called at application startup and shutdown, respectively. A common use of a Plugin Action is to configure or load application-specific data as the web application is starting up.
其中Spring中的ContextLoaderPlugIn就是继承此
PlugIn ,从而引入了Spring
基本上这样子就可以把Spring+Struts配置好,然后Spring+JDBC,关键就是配置好DataSource,然后通过JdbcTemplate类,就可以很方便的进行配置.以下部分为代码:
web.xml
<
web-app
>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>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>
</web-app>
struts-config.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"
>
<
struts-config
>
<
data-sources
/>
<
form-beans
>
<
form-bean
name
="reportBaseForm"
type
="com.sjtu.wgq.framework.controller.ReportBaseActionForm"
>
</
form-bean
>
</
form-beans
>
<
global-exceptions
/>
<
global-forwards
/>
<
action-mappings
>
<
action
name
="reportBaseForm"
path
="/reportBaseAction"
type
="org.springframework.web.struts.DelegatingActionProxy"
>
<
forward
name
="success"
path
="/success.jsp"
></
forward
>
<
forward
name
="fail"
path
="/fail.jsp"
></
forward
>
</
action
>
</
action-mappings
>
<
message-resources
parameter
="com.sjtu.wgq.framework.ApplicationResources"
/>
<
plug-in
className
="org.springframework.web.struts.ContextLoaderPlugIn"
>
<
set-property
property
="contextConfigLocation"
value
="/WEB-INF/action-servlet.xml"
/>
</
plug-in
>
</
struts-config
>
action-servlet.xml
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<!
DOCTYPE beans PUBLIC
"
-//SPRING//DTD BEAN//EN
"
"
http://www.springframework.org/dtd/spring-beans.dtd
"
>
<
beans
>
<
bean name
=
"
/reportBaseAction
"
class
=
"
com.sjtu.wgq.framework.controller.ReportBaseAction
"
></
bean
>
</
beans
>
applicationContext.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
<
bean
id
="dataSource"
class
="org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<
property
name
="driverClassName"
value
="oracle.jdbc.driver.OracleDriver"
>
</
property
>
<
property
name
="url"
value
="jdbc:oracle:thin:@IP:端口:SIN"
>
</
property
>
<
property
name
="username"
value
=""
></
property
>
<
property
name
="password"
value
=""
></
property
>
</
bean
>
</
beans
>
applicatoinContext-dao.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
<
bean
id
="userDAO"
class
="com.sjtu.wgq.dao.jdbc.UserJdbcTemplateDAO"
>
<
property
name
="dataSource"
>
<
ref
bean
="dataSource"
/>
</
property
>
</
bean
>
</
beans
>
UserDAO.java
public
interface
UserDAO
...
{
public List getUser(String uid);
}
User.java
public
class
User
...
{
String name;
String pwd;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getPwd() ...{
return pwd;
}
public void setPwd(String pwd) ...{
this.pwd = pwd;
}
}
UserJdbcTemplateDAO.java
public
class
UserJdbcTemplateDAO
extends
JdbcDaoSupport
implements
UserDAO
...
{
public List getUser(String uid)...{
if(uid!=null&& !uid.equals(""))...{
String sql = "select ba.USER_ID id ,ba.PSWD_NO no from Ba.Ba9040 ba where ba.USER_ID= " +uid;
//this.getJdbcTemplate().queryForList(sql);
return this.getJdbcTemplate().query(sql,new UserRowMapper());
}
return null;
}
private class UserRowMapper implements RowMapper...{
public Object mapRow(ResultSet rs,int index)throws SQLException...{
User user =new User();
user.setName(rs.getString("id"));
user.setPwd(rs.getString("no"));
return user;
}
}
}
ReportBaseAction.java
public
abstract
class
BaseAction
extends
ActionSupport {
/**
继承与ActionSupport,
*
@see
org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
&nbs