项目需要,在原来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
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
>
![](http://img.e-com-net.com/image/info8/3021fccc4e39443e82f669e757b188bf.gif)
<param-name>contextConfigLocationparam-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-*.xmlparam-value>
context-param>
![](http://img.e-com-net.com/image/info8/3021fccc4e39443e82f669e757b188bf.gif)
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
![](http://img.e-com-net.com/image/info8/3021fccc4e39443e82f669e757b188bf.gif)
<servlet>
<servlet-name>actionservlet-name>
<servlet-class>org.apache.struts.action.ActionServletservlet-class>
<init-param>
<param-name>configparam-name>
<param-value>/WEB-INF/struts-config.xmlparam-value>
init-param>
<init-param>
<param-name>debugparam-name>
<param-value>3param-value>
init-param>
<init-param>
<param-name>detailparam-name>
<param-value>3param-value>
init-param>
<load-on-startup>0load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>actionservlet-name>
<url-pattern>*.dourl-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"
>
![](http://img.e-com-net.com/image/info8/3021fccc4e39443e82f669e757b188bf.gif)
<
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
...
{
![](http://img.e-com-net.com/image/info8/30b3da7787b24a2b9dc5b44c8de3f160.gif)
String name;
String pwd;
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
public String getName() ...{
return name;
}
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
public void setName(String name) ...{
this.name = name;
}
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
public String getPwd() ...{
return pwd;
}
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
public void setPwd(String pwd) ...{
this.pwd = pwd;
}
}
UserJdbcTemplateDAO.java
public
class
UserJdbcTemplateDAO
extends
JdbcDaoSupport
implements
UserDAO
...
{
![](http://img.e-com-net.com/image/info8/30b3da7787b24a2b9dc5b44c8de3f160.gif)
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
public List getUser(String uid)...{
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
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;
}
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
private class UserRowMapper implements RowMapper...{
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
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)
*/
public
final
ActionForward execute(ActionMapping mapping,ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) {
ApplicationContext ctx
=
this
.getWebApplicationContext();
//
Spring完成注入和初始化.
UserDAO userDao
=
(UserDAO)ctx.getBean(
"
UserDAO
"
);
//
调用UserDAO接口声明的方法.
//
userDAO.getUser("1");
}
}
ActionForm的部分就省略了.
另外提一下就是,ActionSupport的getWebApplicatoinContext()方法载入的xml文件,是要在web.xml的
<
context-param
>声明的,否则如果只在struts-config.xml声明会找不到的.因为两个声明的不是一个概念,在getWebApplicatoinContext是通过servlet.getContext 的方法找到的,而在struts-config.xml配置的只是action-servlet.xml,即action的内容.具体原因,我还没有去查.
另外就是关于多个applicationContext的问题,可以参考web.xml的配置就可以了.
还有,applicationContext-dao.xml中的UserDao引用了dataSource,因为不在同一文件中,所以用
的格式,如果在同一文件可以用来引用.
通过Spring,使得Struts更加灵活,JDBC的配置很简单,但配置文件多,而且因为applicatonContext配置不正确的话,web工程是无法启动的,报linstener的错误,但不会具体指出是什么错.
还有以下问题要看看资料:
1,为什么在plugin里配置的xml,无法在getWebApplication中得到,按道理应该是一致的,因为都是通过actoinservlet去取得信息.
2,RequestProcessor具体是怎么工作的.
下次把<程序员面试指南>的体会说一下.