tags:

views:

156

answers:

2

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.

A: 

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
Script Runner
Yes I have this property set. But I cannot convince Struts2 to use bean created by Spring as action mapper. I was able to do this when we used webwork, now I try to migrate to Struts and cannot make this work.
Ula Krukar
Can you post your configuration files ( struts and spring )?
Script Runner
Turns out I had it all wrong. I added this property and struts2-spring-plugin-x.x.x and now it works fine.
Ula Krukar
A: 

I'm having the same issue. I have a custom action mapper which I've configured Struts 2 to use in the struts.xml:

    <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="PracticeContextMapper" class="com.practs.struts.mapper.PracticeContextMapper" />
<constant name="struts.mapper.composite" value="PracticeContextMapper,struts" />
<constant name="struts.mapper.class" value="composite" />

Then, I put in an autowired property (with accessor methods) in my new custom action mapper:

public class PracticeContextMapper implements ActionMapper
{
@Autowired
private PracticeService practiceService;

@Override
public ActionMapping getMapping(HttpServletRequest request,
        ConfigurationManager configManager)
{
        ...

The service bean I want to inject is set up in my applicationContext file propertly:

<bean id="practiceService" class="com.practs.bizObjects.practices.PracticeServiceImpl" />

I'm doing the exact same thing with all my struts actions, and Spring is injecting everything just fine. Unfortunately, it won't do any injection on the ActionMapper. Is this a limitation of the Struts-Spring plugin? Am I missing something?

Thanks in advance.

Robert Nelson
This should be a separate question. Here it goes:- define your action mapper as a bean,- provide the name of this bean as a class attribute in your configuration.This way action mapper will be created by Spring (in your current configuration it isn't) and it should work just fine.
Ula Krukar
Ula Krukar
I've tried to do as you say - add it as a bean in my Spring config and reference the bean ID in the Struts config - but I get a "java.lang.ClassNotFoundException: practiceContextMapperBean" when I start up Struts. Thoughts?
Robert Nelson