This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Intergration of Struts2 and EJB3.0 using struts2-ejb3-plugin Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Intergration of Struts2 and EJB3.0 using struts2-ejb3-plugin" Watch "Intergration of Struts2 and EJB3.0 using struts2-ejb3-plugin" New topic
Author

Intergration of Struts2 and EJB3.0 using struts2-ejb3-plugin

Agrael Lee
Greenhorn

Joined: May 21, 2010
Posts: 5
I've worked on projects in the past using Struts2 and EJB3.0, and found it very painful to locate EJBs inside Struts2 Actions and Interceptors using context.lookup. My attempt to find an intergration tool for the two technologies ended without much luck. There were two EJB3.0 plugins in the Apache Struts2 plugins repositry, which completes the intergration by the use of Annotations. However, these plugins introduced undesired third-party libraries and used static JNDI reference paths. I ended up writing this plugin to simulate application server rules and EJB component life cycle.

This article is about how to use struts2-ejb3-plugin for intergrated development between Struts2 and EJB3.0.

You can learn about this plugin from the following sources:
+ Apache Struts2 Plugins Repository (http://cwiki.apache.org/S2PLUGINS/home.html)
+ strus2-ejb3-plugin Official Website (http://cwiki.apache.org/S2PLUGINS/struts2-ejb3-plugin.html)
+ Google Code (http://code.google.com/p/struts2-ejb3-plugin/)

==========

Plugin Installation:

Installation of struts2-ejb3-plugin is relatively easy, thanks to Strut2's excellent plugin mechanism. Simply place struts2-ejb3-plugin.jar into the /WEB-INF/lib directory of the required webapp. You would also need to create a jndi.properties file under the classpath of the running webapp to configure JNDI settings for the plugin.

==========

Examples

Let's start by creating a few SessionBeans:















Here are some Interceptors:





The SessionBeans and Interceptors above will print out some debug text when they are executed.

Here is the focus of this article: a Struts2 Action:



The SessionBeans we wrote previously are injected to this Struts2 Action via the @EJB annotation. This plugin supports all Interceptors except the default global interceptors. Currently, @PostConstruct annotated methods are the only life cycle related methods that are supported by this plugin.

struts2-ejb3-plugin completes the integration between Struts2 and EJB3.0 by simulating application server rules and EJB component lifecycle, so there is no need to import third-party libraries.

struts2-ejb3-plugin also works with Struts2's Interceptors. When a method uses both Struts2 Interceptor and the EJB Interceptors, EJB Interceptors will execute before and destroyed after Struts2 Interceptor.

Let's take a look at the example Struts2 Interceptor below:



As we can see, not only Action fields can be annotated, Action lifecycle related methods can also be intercepted using the @Interceptors annotation.

==========

Plugin Settings

The default settings file is: cn/agrael/struts/plugin/ejb3/default-struts-ejb3-plugin.properties

Settings are as follows:



You can customize these settings by creating a struts-ejb3-plugin.properties file in the webapp's classpath to override the default settings.

==========

Notes

@PreDestory methods are currently not supported yet.

Glassfish 2.x and Jboss are the only supported web containers. Will gradually add support for other web containers.

==========

That's all for now. You can download the plugin from here:
(http://struts2-ejb3-plugin.googlecode.com/files/struts2-ejb3-plugin-demo.zip)
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

Pretty interesting.
I already tested these EJB 3 plugins for Struts 2. Thanks for this new one that I will check out soon.
By the way, how come there are some Chinese characters in this example and in the sites you pointed at ?


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Agrael Lee
Greenhorn

Joined: May 21, 2010
Posts: 5
Celinio Fernandes wrote:Pretty interesting.
I already tested these EJB 3 plugins for Struts 2. Thanks for this new one that I will check out soon.
By the way, how come there are some Chinese characters in this example and in the sites you pointed at ?



Thank you for your interest.
The sites mentioned in this article have Chinese characters because I'm Chinese, they'll eventually be translated into English. In fact, this article was originally written in Chinese, and have been translated into English.
Agrael Lee
Greenhorn

Joined: May 21, 2010
Posts: 5
Error correction:



from DemoAction should be changed to:

amine cherif
Greenhorn

Joined: Oct 08, 2010
Posts: 1
Hi,

Thank you lee for this article. However, as i am new struts developer, i have some general and specific questions:
- is there another way to use EJB3 in struts 2 (without using plugin) ?
- here is it necessary to define interceptors in order to use EJB3 ?
- @EJB
public void setDemoMethodInjectRemote(
DemoMethodInjectRemote demoMethodInjectRemote) {
this.demoMethodInjectRemote = demoMethodInjectRemote;
}
is it correct to make @EJB annotation before a method ?
- @Interceptors(DemoInterceptor1.class)
public class DemoAction extends ActionSupport {
is this means that all DemoAction methods will be invoked by DemoInterceptor1 interceptor ?

Thank you in advance for the responses
Agrael Lee
Greenhorn

Joined: May 21, 2010
Posts: 5
amine cherif wrote:Hi,

Thank you lee for this article. However, as i am new struts developer, i have some general and specific questions:
- is there another way to use EJB3 in struts 2 (without using plugin) ?
- here is it necessary to define interceptors in order to use EJB3 ?
- @EJB
public void setDemoMethodInjectRemote(
DemoMethodInjectRemote demoMethodInjectRemote) {
this.demoMethodInjectRemote = demoMethodInjectRemote;
}
is it correct to make @EJB annotation before a method ?
- @Interceptors(DemoInterceptor1.class)
public class DemoAction extends ActionSupport {
is this means that all DemoAction methods will be invoked by DemoInterceptor1 interceptor ?

Thank you in advance for the responses




1、is there another way to use EJB3 in struts 2 (without using plugin) ?
R:You can use interceptors to inject the EJB.

2、here is it necessary to define interceptors in order to use EJB3 ?
R:struts2-ejb3-plugin without using Interceptor.

3、is it correct to make @EJB annotation before a method ?
R:y

4、is this means that all DemoAction methods will be invoked by DemoInterceptor1 interceptor ?
R:y,@PreDestory methods are currently not supported yet. Others follow the EJB specification。
karunaja karan
Greenhorn

Joined: Oct 28, 2012
Posts: 3
hi ,

I am getting following error , please any one help me

- Jboss 5 is Application server.
- vendormgmt is ear file name
-following property values are contains by struts-ejb3-plugin.properties
earFileBaseName=vendormgmt



16:14:07,355 INFO [STDOUT] 2012-10-28 16:14:07,354 WARN com.opensymphony.xwork2.config.providers.InterceptorBuilder.warn:56 - Unable to load config class cn.agrael.sepdemo.interceptor.DemoStrutsInterceptor at Class: cn.agrael.struts.plugin.ejb3.AbstractApplicationServer
File: AbstractApplicationServer.java
Method: lookup
Line: 107 - cn/agrael/struts/plugin/ejb3/AbstractApplicationServer.java:107:-1 probably due to a missing jar, which might be fine if you never plan to use the demoInterceptor interceptor
16:14:07,359 INFO [STDOUT] 2012-10-28 16:14:07,357 ERROR com.opensymphony.xwork2.config.providers.InterceptorBuilder.error:38 - Actual exception
[ENCJNDIName = java:comp/env/cn.agrael.sepdemo.interceptor.DemoStrutsInterceptor/demoRemote][defaultJNDIName = vendormgmt/DemoRemote/remote]??? - Class: cn.agrael.struts.plugin.ejb3.AbstractApplicationServer
File: AbstractApplicationServer.java
Method: lookup
Line: 107 - cn/agrael/struts/plugin/ejb3/AbstractApplicationServer.java:107:-1
at cn.agrael.struts.plugin.ejb3.EJBObjectFactory.buildInterceptor(EJBObjectFactory.java:36)
at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:70)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.lookupInterceptorReference(XmlConfigurationProvider.java:1043)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildInterceptorList(XmlConfigurationProvider.java:570)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:392)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:495)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:286)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:234)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:234)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:332)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:90)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3783)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4413)
at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeployInternal(TomcatDeployment.java:310)
at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeploy(TomcatDeployment.java:142)
at org.jboss.web.deployers.AbstractWarDeployment.start(AbstractWarDeployment.java:461)
at org.jboss.web.deployers.WebModule.startModule(WebModule.java:118)
at org.jboss.web.deployers.WebModule.start(WebModule.java:97)
at sun.reflect.GeneratedMethodAccessor434.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:668)
at org.jboss.system.microcontainer.ServiceProxy.invoke(ServiceProxy.java:206)
at $Proxy38.start(Unknown Source)
at org.jboss.system.microcontainer.StartStopLifecycleAction.installAction(StartStopLifecycleAction.java:42)
at org.jboss.system.microcontainer.StartStopLifecycleAction.installAction(StartStopLifecycleAction.java:37)
at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62)
at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71)
at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51)
at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
at org.jboss.system.microcontainer.ServiceControllerContext.install(ServiceControllerContext.java:286)
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1631)
at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
karunaja karan
Greenhorn

Joined: Oct 28, 2012
Posts: 3

please any one help me to get out of following error.


javax.ejb.EJBException: javax.naming.NamingException: [ENCJNDIName = java:comp/env/cn.agrael.sepdemo.action.DemoAction/remote][defaultJNDIName = vendormgmt/DemoRemote/remote]没有找到对应资源。
cn.agrael.struts.plugin.ejb3.StrutsEJBActionProxyFactory.createActionProxy(StrutsEJBActionProxyFactory.java:33)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:501)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
root cause

javax.naming.NamingException: [ENCJNDIName = java:comp/env/cn.agrael.sepdemo.action.DemoAction/remote][defaultJNDIName = vendormgmt/DemoRemote/remote]没有找到对应资源。
cn.agrael.struts.plugin.ejb3.AbstractApplicationServer.lookup(AbstractApplicationServer.java:107)
cn.agrael.struts.plugin.ejb3.AbstractApplicationServer.methodEJBAnnotationLookup(AbstractApplicationServer.java:41)
cn.agrael.struts.plugin.ejb3.StrutsEJBUtils.methodInject(StrutsEJBUtils.java:429)
cn.agrael.struts.plugin.ejb3.StrutsEJBUtils.executeEJBInjectAnnotation(StrutsEJBUtils.java:275)
cn.agrael.struts.plugin.ejb3.StrutsEJBActionProxyFactory.createActionProxy(StrutsEJBActionProxyFactory.java:25)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:501)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
karunaja karan
Greenhorn

Joined: Oct 28, 2012
Posts: 3
if any one knows please help me , How jndi name is framed by this plugin .
because, jboos gives the jndi name like earfilename/sessionbean_name/remote or local
but plugin looking earfilename/session_name(Interface name)/remote or local.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Intergration of Struts2 and EJB3.0 using struts2-ejb3-plugin
 
Similar Threads
how many interceptor method in a Bean class ?
dobut in protected access
compound assignment operator behaving differently for Byte and byte
constructor
EJB3 mock questions available now! (Section 2)