Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Architecture & Framework » Spring Framework  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:[求助][急]如何将活动的servlet 注入 bean里 [Re:cliff]
cliff





发贴: 11
积分: 0
于 2006-10-28 12:07 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
"If indeed there is a need, maybe the other way around is better, do it in the servlet context, i.e., either in the servlet or in the interceptor."
about this sentence, I am confused! I know what you mean, you mean handle the action in the servlet if you want to use the servlet, as you don't need to care about where the servlet is , it's just there.

Let's consider a scenario:
With struts, you have more then 10 action classes(inheriting DispatchAction), in each action you have more than 5 action methods. You define a common class which have all the common methods, and you have to apply all these methods of this class in each method of every action class. But you have to set the ActionServlet as the property of this common class, since you have to get the session attribute in this common methods. One way to inject ActionServlet to this common class is to pass the servlet property of Action(we know action has a property named servlet which represents the ActionServlet instance) to it in each method of each action. It's ugly to do so, everytime you add a new method in the action class, you have to copy the code of injecting servlet and call the common methods of the common class.

Another approach is to use interceptor, just as you say. You can implement MethodBeforeAdvice to setup the interceptor and do the logic in the before() method.


public class SomeInterceptor implements MethodBeforeAdvice {

public void before(Method method, Object[] objects, Object o)
throws Throwable {

ActionMapping mapping=(ActionMapping)objects[0];
ActionForm form=(ActionForm)objects[1];
HttpServletRequest request=(HttpServletRequest)objects[2];
HttpServletResponse response=(HttpServletResponse)objects[3];

DispatchAction action=(DispatchAction)o;
ActionServlet servlet=(ActionServlet)action.getServlet();
...

}
}




public class SomeAction extends DispatchAction {

...

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

...
}



we set this SomeInterceptor into every action method like somefunction()

checking these 2 snippets, you should notice several things:
1. the return type of the before() method is void.
2. the return type of somefunction() mnthod is ActionForward.
3. to get the ActionServlet context in the Interceptor, spring has inject them in the parameters of the before method, so you can hardcode to get them, ok, this is fine, but not stable, we can not make sure each time the parameters are just like these ones in every type of server(tomcat, weblogic, websphere ...).
4. in before() meothd , after checking some logic with these parameters, if some conditions are satisfied, we don't want to continue the process of somefunction() method, we just want to end up the somefunction() and return ActionForward. In before() method, how can we indicate somefunction() method to end up and return the ActionForward. With Spring AOP, how can we do that?

Please give more advices, thank you.



cliff edited on 2006-10-28 12:13


从Coding Fan到真正的技术专家

话题树型展开
人气 标题 作者 字数 发贴时间
29053 [求助][急]如何将活动的servlet 注入 bean里 cliff 603 2006-10-27 15:53
24111 Re:[求助][急]如何将活动的servlet 注入 bean里 floater 30 2006-10-27 21:34
24252 Re:[求助][急]如何将活动的servlet 注入 bean里 cliff 859 2006-10-27 23:21
23981 Re:[求助][急]如何将活动的servlet 注入 bean里 floater 186 2006-10-31 23:07
24004 Re:[求助][急]如何将活动的servlet 注入 bean里 cliff 347 2006-11-01 10:17
28475 Re:[求助][急]如何将活动的servlet 注入 bean里 floater 39 2006-11-01 22:15
23929 Re:[求助][急]如何将活动的servlet 注入 bean里 floater 1351 2006-10-28 02:05
24087 Re:[求助][急]如何将活动的servlet 注入 bean里 cliff 3300 2006-10-28 12:07
23884 Re:[求助][急]如何将活动的servlet 注入 bean里 floater 1150 2006-10-28 23:06
24196 Re:[求助][急]如何将活动的servlet 注入 bean里 cliff 498 2006-10-29 01:14
23997 Re:[求助][急]如何将活动的servlet 注入 bean里 floater 422 2006-10-29 23:32
24332 Re:[求助][急]如何将活动的servlet 注入 bean里 cliff 2218 2006-10-30 14:33
23926 Re:[求助][急]如何将活动的servlet 注入 bean里 floater 56 2006-10-30 22:25
24165 Re:[求助][急]如何将活动的servlet 注入 bean里 cliff 123 2006-10-31 10:00
23893 Re:[求助][急]如何将活动的servlet 注入 bean里 floater 142 2006-10-31 22:58
23959 Re:[求助][急]如何将活动的servlet 注入 bean里 cliff 669 2006-11-01 10:09

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923