Struts2: custom action mapper defined in Spring - spring

I have following situation:
I use Struts2 as MVC cobtroller
I use Spring as object factory
I implemented custom action mapper, I have configured this as a bean in my spring configuration.
How can I tell Struts to use this bean as an action mapper?
I tried top set:
struts.mapper.class=beanName
in struts.properties but this doesn't work.

Do you have have following the struts.xml file ? This is required to tell Struts2 that objects will be created by Spring
<struts>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
...
</struts>
OR
Add the following property in the struts.properties
struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory

Related

Inject Spring config file into Ejb using Annotation

Am trying to migrate project from ejb2.1 to ejb3.1. In my current project's ejb-jar.xml, i am using for loading the spring configuration xml file{which initializes the beans which going to be called in ejbbean class in onEjbCreate() method using "getBeanFactory().getBean("somespringclass") }.
Sample environment entry: <env-entry>
<env-entry-name>ejb/BeanFactoryPath</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>config/xyz.xml</env-entry-value>
</env-entry>
what is the ideal syntax for using annotation(ejb3.1) in ejbbean class so that i can remove in ejb-jar.xml.
Thanks in advance.
Just annotate your EJB-3.1 interface with #EJB and put the following in your Spring configuration XML:
<context:component-scan base-package="<your-ejb-package-e.g.-com.ejb.sample>" />

Spring Webflow model validation.

We have developed an internet bank project in our company. We have used spring-mvc in this project and all of property validations have been carried out using Hibernate validator. However for some of our services that have a state-machine nature, we need to use spring-webflow.
In doing so we need to validate models in spring webflow without adding any extra validators. As you know There are 2 ways to validate a model programmatically:
Define validation logic in the model object.
Define a separate object called Validator to validate the model.
However, we DO NOT want to use none of these validation techniques.
For example lets imagine we had a Class named Customer. We used annotation validation for the properties in this class in our project. Now, we want to use this class as a model in view states in our webflow.
I was wondering if someone could help us how to validate models in spring webflow using annotation validation with hibernate validator.
Spring Webflow 2.4 supports JSR-303 validation and partial validation through the use of validation groups.
You need to add the following to the application context xml file:
<webflow:flow-registry flow-builder-services="flowBuilderServices" />
<webflow:flow-builder-services id="flowBuilderServices" validator="validator" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
This will allow you to validate your model objects using annotations.

Configuration to do the spring dependency injection in action class in struts 2?

i have legacy j2ee project which is using struts 2 with spring. Now when i put debugger in first method call in action
class, i find all instance variables dependecies are injected automatically. i mean where do we configure dependency
injection for action class in struts? i explored the web.xml too , i do not find any related stuff?
Does your struts configuration (struts.xml) contain an element like this?:
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
Read more at http://struts.apache.org/2.2.3/docs/spring-plugin.html
If Spring is beiing used to inject the Dependencies for struts2 which includes results/actions etc.that means your code must be using Struts2-Spring plugin.Struts2 by default use its own Object factor to create instances of Action classes/ Results and Interceptors etc.
In order to use Spring one need to tell Struts2 about which object creation factory to be used.for that we need to define the following entry in to either struts.xml file or struts.properties file
struts.xml
<struts>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
...
</struts>
struts.properties
struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory
I belive that in your web.xml you will find an entry to Spring's ContextLoaderListener which will load the applicationContext xml file from the class path.
when uusing Spring the action will be create by spring in its xml file and in the action configuration file the bean reference is being used in place of Action Class name.
So all you need to check applicationContext.xml in your Project where Action will be initiated as a prototype beans and those bean references being used in struts.xml file to defining your Struts2 Action class

Autowiring Struts Action Classes with Spring

I have a question about spring and struts.
Currently I have spring injecting my struts action classes for me.
I was experimenting and trying to get Spring to inject my Struts action classes
for me using autowiring.
I have my spring applicationContext config file scanning the base package that the
action class is in using context:component-scan base-package="my.package",
and im using #Component annotation at the action classes class level.
Im also using #Qualifier("myActionClass") at the same action classes class level.
Im not configuring the action class as a Spring bean in applicationContext.
Then in my struts.xml config file, while configuring my action class, instead of giving the fully qualified package and class name, I use the #Qualifier annotation name "myActionClass".
This doesnt work though.
If in my applicationContext config file, configure my action class as a spring bean, get rid of the #Component and #Qualifier annotation on the action class, and in struts.xml, put the action classes Spring bean id for the class, then Spring injects my action class for me and everything is dandy. Only, this isnt using Autowiring the action class, and thats what I was testing.
Anyone know if autowiring using context:component-scan base-package
to scan your packages for your action classes so you dont have to configure them in applicationContext is possible?
Everything is explained in Spring documentation: Apache Struts 1.x and 2.x.
I am not sure whether you are using Struts 1 or 2. For Struts 1 you had to add Spring plugin to Struts configuration (I know it works). In Struts 2 all actions are created by Spring hence they are fully capable of Spring injection like all other beans.
Struts 2 seems to rely on there being a spring bean with the same spring bean-name matching the action class name (full name with package). You can specify the bean name in the #Component annotation, and it's also possible to make a global user-defined bean naming strategy so you can avoid adding this information to all your beans

JSF Spring Bean set property

i have a JSF web application. I use Beans as Spring Beans (not JSF managed beans). Now i have an URL to application www.example.com?parameter=2
I would like to set this parameter into bean on the page load. I now how to do this with spring web flow but with JSF Navigation i cant do this.
What do you think about using JSTL c:set or jsp:setProperty?
Thanks for your help.
Kind regards
Sebastian
From here:
One could extend a Springs org.springframework.beans.factory.config.PropertyPlaceholderConfigurer which accesses the RequestContext (org.springframework.web.context.request.RequestContextHolder#getRequestAttributes()) to resolve ${xyz}-like properties in the bean.
Of course that would only work for Spring beans with “request”-scope.
If the bean is in session-scope you could simply use the following in a Phase Listener method:
property = FacesContezt.getCurrentInstance().getExternalContext
.getRequestMap().get("paramName");
the phase listener is defined with
<f:view beforePhase="#{bean.method}">
if using facelets, its beforePhaseListener
If you were using faces-context.xml, you could've used the <managed-property>.

Resources