`

使用 ActionSupport 代替 Action

阅读更多

另外,还有一种方法可以用于 Spring 与 Struts 的整合:让 Action 在程序中手动获得ApplicationContext 实例。在这种整合策略下, Struts 的 Action 不接受 IoC 容器管理,使Action 的代码与 Spring API 部分稿合,但造成代码污染。

这种策略也有其好处:代码的可读性非常强,在 Action 的代码中显式调用业务逻辑组件时,无须等待容器注入。

在 Action 中访问 ApplicationContext 有以下两种方法:

利用 WebApplicationContextUtils 工具类。

利用 ActionSupport 支持类。

WebApplicationContextUtils 可以通过 ServletContext 获得 Spring 容器实例。而ActionSupport 类则提供一个更简单的方法:getWebApplicationContextO ,该方法用于获取 ApplicationContext 实例。

Spring 扩展了 Struts 的标准 Action 类,可在其 Struts 的 Action 后加上 Support,Spring的 Action 有如下几种:

ActionSupport。
DispatchActionSupport。
LookupDispatchActionSupporto
MappingDispatchActionSupport0

下面分别给出利用 ActionSupport 的示例代码:

新的业务控制器,继承 Spring 的 ActionSupport 类

public class LoginAction extends ActionSupport
//依然将ValidBean作为成员变量
privateValidBean vb;
//构造器,注意:不可在构造器中调用getWebApplicationContext()方法
public LoginAction()
//完成ValidBean的初始化
public ValidBean getVb()
return(ValidBean)getWebApplicationContext() .getBean("vb") ;
//必须重写该核心方法,该方法 actionForm将表单的请求参数封装成值对象
public ActionForward execute(ActionMapping mapping,
ActionForm form,HttpServletRequest request, 
HttpServletResponse response)throws Exception
Ilform由 ActionServlet转发请求时创建,包装了所有的请求参数
LoginForm loginForm = (LoginForm)form;
//获取 username请求参数
String username = loginForm.getUsername();
//获取 pass 请求参数
String pass = loginForm.getPass();
//下面作服务器端的数据校验
String errMsg =
//判断用户名不能为空
if (username -- null // username.equals(""))
errMsg:t-= "您的用户名丢失或没有输入,请重新输入 //;
//判断密码不能为空
else if (pass == null I I pass.equals("") )
errMsg += "您的密码丢失或没有输入,请重新输入";
//如果用户名和密码不为空,则调用业务组件
else
//vb是业务逻辑组件,通过上面的初始化方法获得
if (getVb() .valid(username,pass))
return mapping.findForward("welcome");
else
errMsg =什盔的用户名和密码不匹配//i
//判断是否生成了错误信息,
if (errMsg != null&&!errMsg.equals(""))
//将错误信息保存在 request 里,则跳转到 input 对应的 forward对象
request.setAttribute("err", errMsg);returnmapping.findForward("input");
else
//如果没有错误信息,跳转到welcome对应的 forward对象
return mapping.findForward("welcome") ;


在这种整合策略下,表现层的控制器组件不再接受 Joe 容器管理,因此没有了控制器 context。应将原有的 action-servlet.xml 文件删除,井修改 plug-in 元素,但不要加载该文件。另外,还要修改其 action 配置,将 action 配置的 type 元素修改成实际的处理类。这种整合策略也有个好处,提高了代码的可读性,对传统 Struts 应用开发的改变很小,容易使用。

将该 Action 部署在 struts-config.xml 中,此时 Struts 将负责创建该 Action。struts-config.xml 文件的源代码如下:

<!--XML文件版本,编码集--〉
<?xml version="l.O"encoding="gb2312"?>
<!--struts配置文件的文件头,包括dtd 等信息>
<!DOCTYPE struts-config PUBLIC
'-//ApacheSoftware FoundationllDTD Struts Configuration 
1.21IEN"''http://struts.apache.org/dtds/struts-config_l_2.dtd''>
<!--struts配置文件的根元素>
<struts-config>
<!-- 配置 formbean. 所有的 formbean都放在 form-beans元素里定义-->
<form-beans>
<'定义了一个formbean. 确定 formbean名和实现类--〉
<form-bean name="loginForm" type="lee.LoginForm"l></form-beans>
<!-- 定义 action部分,所有的action都放在 action-mapping元素里定义>
<act~on-mappings>
<!--这里只定义了一个actiono action 的类型为ActionSuport的子类--〉
<action path="/login" type="type="lee.LoginAction"
name="loginForm"scope="request" 飞ralidate="true"input="/login.jsp" >
<!--定义 action 内的两个局部 forward元素--〉
<forward name="input" path="/login.jsp"l><forward 
name="welcome" path="/welcome.html"l></action></action-mappings>
<!加载国际化的资源包…〉
<message-resources parameter="mess"l>
<!--- 装载验证的资源文件--〉
<plug-in className="org.apache.struts.validator.
ValidatorPlugIn"><set-property property="pathnames" 
value="IWEB-INFIvalidator-rules.xml, /WEB-INF/
validation.xml"I><set-propertyproperty=町 
stopOnFirstError"value="true"l></plug-in></struts-config>

此时,这种配置方式非常简单,Spring无须使用配置Action的配置文件,只需要业务逻辑组件的配置文件,其业务逻辑组件的配置文件如下:

<?xml version="1.0" encoding="gb2312"?>
<J指定 Spring 配置文件的 dtd>
<lDOCTYPE beans PUBLIC"-//SPRiNG//DTDBEAN//EN"
''http://www.springframework.org/dtd/spring-beans.dtd''>
<!-- spring配置文件的根元素->
<beans>
<!-配置ValidBean实例-->
<bean id="vb" class="lee.ValidBeanImpl"l>
</beans>

该配置文件中的业务逻辑组件由Spring 容器负责实现,而ActionSupport能够先定位 Spring容器,然后获得容器的业务逻辑组件。这种整合策略与前面两种整合策略的执行效果完全相同。

从代码中我们可以看出,在这种整合策略下,业务控制器再次退回到Struts 起初的设计,仅由 struts-config.xml中 Action充当,可以只创建实际的Action实例,而避免了如 DelegatingActionProxy整合策略的性能低下,但这种整合策略的代价是污染了代码。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics