论坛首页 Java版 Spring

请教SS中DelegatingActionProxy代理问题

浏览 3812 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2006-01-29
通常在设计中往往会要求同一个Action来处理客户端发出的不同请求,如下
[code:1]
<a href="users.do&operation=deleteUsr">删除一个成员</a>
<a href="users.do&operation=addUsr">添加一个成员</a>
[/code:1]
碰到该类问题,在struts中得到了很好的处理,如下
sturts-config.xml配置
[code:1]
<action type="test.struts.dispatch.UserAction" name="UserForm"  parameter="operation" scope="request" path="/users" input="/users.jsp">
<forward name="del_succ" path="/del_succ.jsp"/>
<forward name="ins_succ" path="/ins_succ.jsp"/>
<forward name="failed" path="/error.jsp"/>
</action>
[/code:1]
UserAction代码
[code:1]
public class UserAction extends DispatchAction {
public UserAction() {

}

public ActionForward deleteUsr(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
......
}

public ActionForward addUsr(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
......
}
[/code:1]
在struts中DispatchAction 很好的解决了此类问题。可是当spring+sturst结合的过程中,如果碰到上面的问题,首先改写struts-config.xml
[code:1]
<action path="/users" type="org.springframework.web.struts.DelegatingActionProxy" name="UserForm" scope="request">
<forward name="del_succ" path="/del_succ.jsp"/>
<forward name="ins_succ" path="/ins_succ.jsp"/>
</action>
[/code:1]
显而易见,在面向SS组合的配置方式中,用spring提供的DelegatingActionProxy 作为Action
的type属性。DelegatingActionProxy同样是org.apache.struts.action.Action的一个子类,它将
把调用请求转交给真正的Action实现.如下
[code:1]
<bean name="/users" class="classmate.LoginAction" singleton="false">
<property name="userDAO">
<ref bean="userDAOProxy" />
</property>
</bean>
[/code:1]
DelegatingActionProxy则实现了针对实际Action的调用代理,struts最终调用的将是由spring管理的Action实例,这样客户端发送的各种请求就可以用spring的Ioc设计思想实现了。可是在我发现并不是我预想的那样结果,总是出现source not found。程序无法执行下去,原因很简单就是在
[code:1]
<action path="/users" type="org.springframework.web.struts.DelegatingActionProxy" name="UserForm" scope="request">
<forward name="del_succ" path="/del_succ.jsp"/>
<forward name="ins_succ" path="/ins_succ.jsp"/>
</action>
[/code:1]
的时候,并没有像struts那样传递一个参数parameter="operation",所以肯定找不到执行哪一个Action的type,我猜想DelegatingActionProxy代理返回的类型一定不会是DispatchAction,于是我查看了DelegatingActionProxy的源码
[code:1]
public class DelegatingActionProxy extends Action {

/**
* Pass the execute call on to the Spring-managed delegate Action.
* @see #getDelegateAction
*/
public ActionForward execute(
ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {

Action delegateAction = getDelegateAction(mapping);
return delegateAction.execute(mapping, form, request, response);
}
......
}
[/code:1]
果然是继承了Action,此时我要实现上述描述的操作,DelegatingActionProxy代理应该如何处理呢?
   
最后更新时间:2006-02-05
改写struts-config.xml的时候,你只需要更改type为org.springframework.web.struts.DelegatingActionProxy,其他的还是按照原来不与spring结合那样写就可以了。
即改写成如下sturts-config.xml配置
[code:1]
<action type="org.springframework.web.struts.DelegatingActionProxy" name="UserForm"  parameter="operation" scope="request" path="/users" input="/users.jsp">
<forward name="del_succ" path="/del_succ.jsp"/>
<forward name="ins_succ" path="/ins_succ.jsp"/>
<forward name="failed" path="/error.jsp"/>
</action>
[/code:1]
   
0 请登录后投票
最后更新时间:2006-02-05
谢谢你的恢复,你的意思就是追加一个parameter="operation"参数的传递,按照你的说法,我试了一下,果然达到了预期的效果,可是我就弄不明白DelegatingActionProxy
代理过后返回的是一个Action类型的,而在UserAction 中又必须继承DispatchAction
[code:1]public class UserAction extends DispatchAction {
        public UserAction() {

        }

        public ActionForward deleteUsr(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                        throws Exception {
......
}

        public ActionForward addUsr(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                        throws Exception {
......
}
[/code:1]
spring是在哪里操作了parameter="operation"参数。谢谢
   
0 请登录后投票
最后更新时间:2006-02-09
SS的这种结合方式耦合度低,便于利用Spring的IoC或AOP特性,正说明Spring对Struts并没有过多的干涉,仅仅是在其环境下来管理Struts Action,即Action的实际运作方式还是按Struts原有方式运行,所以在相应的Action类上还得继承Action类,DispatchAction类其中的一类来完成对应的操作。
所以我觉得Spring并没有操作parameter="operation"分发参数,这是由Struts本身来完成的。
   
0 请登录后投票
最后更新时间:2006-02-09
也就是说除了不直接接触Action以外,其他的部分还是由spring负责。这样的确是可以达到解耦的效果,而我就是想弄明白SS结合过程中通过设置DelegatingActionProxy代理完成。
[code:1]
<action type="org.springframework.web.struts.DelegatingActionProxy" name="UserForm"  parameter="operation" scope="request" path="/users" input="/users.jsp">
[/code:1]
可是在DelegatingActionProxy源码中并没有直接处理parameter="operation"这一个分发参数。spring是在哪里把这个参数告诉给了struts呢?
谢谢
   
0 请登录后投票
最后更新时间:2006-02-09
[code:1]
protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
     WebApplicationContext wac = getWebApplicationContext(getServlet(), mapping.getModuleConfig());
     String beanName = determineActionBeanName(mapping);
     return (Action) wac.getBean(beanName, Action.class);
}

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception {
     Action delegateAction = getDelegateAction(mapping);
     return delegateAction.execute(mapping, form, request, response);
}

[/code:1]
当Spring调用其所管理的Action时,会先根据ActionMapping的配置来实例化Action类,确定Action类型。隐藏字段的名称在通用的ActionMapping 的parameter 属性中传递给Action。DispatchAction 然后会从请求中获取字段的值,并使用反射来调用相应的方法。由此可见,Spring仅仅是实例化所管理的Action类,并没做其它事情。 
   
0 请登录后投票
最后更新时间:2006-02-13
噢。了解!
真实太感谢了
   
0 请登录后投票
论坛首页 Java版 Spring

跳转论坛:
JavaEye推荐