Inject EJB to Struts 2 Action and Custom Validators
来源:互联网 发布:list数据排序 编辑:程序博客网 时间:2024/06/06 02:41
EJB annotation works in container managed instances such as servlet context listeners, servlets, fitlers, and even jsps. It however does not work in struts 2 actions, simply because the actions are not container managed instances and Struts 2 does not implement a hook that makes it happen. To do so, we need a “processor” that at run time checks the annotation, looks up the EJB instance and injects the EJB instance to action. The “processor” is a Struts 2 interceptor that is added to interceptor stack.
Step 1: Create the interceptor:
public class EJBAnnotationProcessorInterceptor implements Interceptor{ public String intercept(ActionInvocation ai) throws Exception { EJBAnnotationProcessor.process(ai.getAction()); return ai.invoke(); } } public class EJBAnnotationProcessor { public static void process(Object instance)throws Exception{ Field[] fields = instance.getClass().getDeclaredFields(); if(fields != null && fields.length > 0){ EJB ejb; Context cxt = null; String jndiName; for(Field f : fields){ ejb = f.getAnnotation(EJB.class); if(ejb != null){ if(cxt == null){ cxt = new InitialContext(); } jndiName = getJndiName(f,ejb); f.setAccessible(true); f.set(instance, cxt.lookup(jndiName)); } } } } }
Step 2: Add the interceptor to interceptor stack in struts.xml:
<package name="base-package" extends="struts-default"> <interceptors> <interceptor name="ejbAnnotationProcessor" class="com.xyz.interceptor.EJBAnnotationProcessorInterceptor"/> <interceptor-stack name="baseStack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="ejbAnnotationProcessor"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="baseStack"/> </package> <package name="package1" extends="base-package"> (action definitions) </package>
Step 3: Annotate your EJB like this:
@Stateless(name="TestSession", mappedName="TestSession") @Remote @Local public class TestSession implements TestInterface{ }
Step 4: Use your EJB in action like this:
@EJB(mappedName="TestSession") private TestInterface test; public void setTest(TestInterface test){ this.test = test; }
As for custom validator, we need to replace struts 2 AnnotationValidatorManager with our implementation that processes EJB annotations.
Step 1: The new validator manager:
public class EJBAnnotationProcessorValidatorManager extends AnnotationActionValidatorManager{ public List<validator> getValidators(Class clazz,String context,String method){ List<validator> validators = super.getValidators(clazz,context,method); for(Validator v : validators){ try{ EJBAnnotationProcessor.process(v); }catch(Exception e){ //proces exception } } return validators; } }
Step 2: Define the new manager and replace struts 2 manager in struts.xml:
<bean type="com.opensymphony.xwork2.validator.ActionValidatorManager" name="ejbAnnotationValidatorManager" class="com.xyz.validator.EJBAnnotationProcessorValidatorManager"/> <constant name="struts.actionValidatorManager" value="ejbAnnotationValidatorManager"/>
Step 3: Use your EJB in custom validator like this:
@EJB(mappedName="TestSession") private TestInterface test; public void setTest(TestInterface test){ this.test = test; }
原文:http://longsystemit.com/javablog/?p=40
- Inject EJB to Struts 2 Action and Custom Validators
- Struts 2 mapping interceptors to action
- Struts 2 mapping interceptors to action
- ejb@Inject注解
- Default Custom Action Locations and IDs
- 转帖:Struts Action and ActionForm
- Struts Action and ActionForm (转)
- How to: Create a Custom Action Specific to a List
- How to remove the action suffix extension in Struts 2
- How to remove the action suffix extension in Struts 2
- 【Struts】Action 2 action类
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- android的AsyncTask类
- 计算机视觉方面的公司-转载
- C#中DateTime类用法大全
- 对象的序列化
- Php生成缩略图
- Inject EJB to Struts 2 Action and Custom Validators
- 网络游戏服务器架构2
- 全新整理:微软、谷歌、百度等公司经典面试100题[第101-160题]
- 相同语句,时间段不一样,不同执行计划分析心得
- BackTrack5 PIN码破解无线路由器
- Java大文件上传
- ubuntu tomcat6 设置JAVA_HOME
- 用JavaScript(js)对时间格式化
- 三大框架整合ssh(二)------DAO层 源码