You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by José Antonio Delgado Trujillo <ja...@gmail.com> on 2016/03/24 14:56:06 UTC

Spring Struts problem

I understood that spring can manage and inject dependency class in Action.
In the example manage and inject EditServiceInMemory.
To do this:
Insert the struts spring plugin dependency in the POM.
Insert the spring listener in the web.xml
Write the configuration file with the bean editService.
Create getter and setter to editService. 

I did it and it run.

But there is an alternative: let Spring to manage also the action class (EditAction).
To do this:
Insert another bean editAction with the property editService in the configuration file.
Change the class in node action of struts.

I did it and it doesn’t run. 
I don’t know why…
The framework can’t instantiate the action class due to it doesn’t know who is the bean editAction.







Un saludo,
José A.

Re: Spring Struts problem

Posted by Lukasz Lenart <lu...@apache.org>.
2016-03-24 22:25 GMT+01:00 José Antonio Delgado Trujillo <ja...@gmail.com>:
>>
>> You must define also scope="prototype" as Struts actions cannot be singletons
>
> There is nothing about that in the tutorial
> https://cwiki.apache.org/confluence/display/WW/Spring+and+Struts+2 <https://cwiki.apache.org/confluence/display/WW/Spring+and+Struts+2>

Ups.... it's a mistake, I will fix it. Actions cannot be singletons as
they must be created on each request. If you will have them as
singletons they will share state between requests/users and this can
produce unpredictable problems

>> You shouldn't mix Struts versions.
>>
> Sure but there is not 2.3.28 version to struts-spring-plugin.
> The new time i’ll use the lasted version 2.5 of the arquetype.

it's there
http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.apache.struts%22%20AND%20a%3A%22struts2-spring-plugin%22

>> Can you share struts.xml as well?
>>
> Upss!!
> I found the mistake, a question of uppercase (in the struts.xml the name of the class action began with lowercase). I’m stupid, i realised when i saw the debug error. SORRY for your time !!!
> Now it runs and without the scope.
>
> <?xml version="1.0" encoding="UTF-8" ?>
> <!DOCTYPE struts PUBLIC
>         "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
>         "http://struts.apache.org/dtds/struts-2.3.dtd">
> <struts>
>
>     <constant name="struts.devMode" value="true"/>
>     <constant name="struts.ui.theme" value="KUTheme"/>
>
>
>     <package name="basicstruts2"  extends="struts-default">
>
>
>
>         <action name="index">
>             <result>/index.jsp</result>
>         </action>
>
>         <action name="edit" class="pfc.struts2.form.action.EditAction" method="input">
>                 <result name="input">/edit.jsp</result>
>         </action>
>
>                 <action name="save" class="pfc.struts2.form.action.editAction" method="execute">
>                         <result name="success">/thankyou.jsp</result>
>                         <result name="input">/edit.jsp</result>
>                 </action>
>     </package>
>
> </struts>

It's ok but you must use action's bean id instead of class name in
struts.xml, as it was shown in the tutorial

<action name="edit" class="editAction" method="input">
    <result name="input">/edit.jsp</result>
</action>

it must be that way as Struts will ask Spring to create a bean based
on "class" attribute - Spring will create an action using just a full
class name but this isn't what you want sometimes, ie. you can have
the same action class used twice but with different dependencies
that's why you should use "editAction" in "class" attribute.


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Spring Struts problem

Posted by José Antonio Delgado Trujillo <ja...@gmail.com>.
> 
> You must define also scope="prototype" as Struts actions cannot be singletons
> 

There is nothing about that in the tutorial
https://cwiki.apache.org/confluence/display/WW/Spring+and+Struts+2 <https://cwiki.apache.org/confluence/display/WW/Spring+and+Struts+2>
> You shouldn't mix Struts versions.
> 
Sure but there is not 2.3.28 version to struts-spring-plugin.
The new time i’ll use the lasted version 2.5 of the arquetype.


> Can you share struts.xml as well?
> 
Upss!!
I found the mistake, a question of uppercase (in the struts.xml the name of the class action began with lowercase). I’m stupid, i realised when i saw the debug error. SORRY for your time !!!
Now it runs and without the scope.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    
    <constant name="struts.devMode" value="true"/>
    <constant name="struts.ui.theme" value="KUTheme"/>

    
    <package name="basicstruts2"  extends="struts-default">

       

        <action name="index">
            <result>/index.jsp</result>
        </action>
        
        <action name="edit" class="pfc.struts2.form.action.EditAction" method="input">
        	<result name="input">/edit.jsp</result>
        </action>

		<action name="save" class="pfc.struts2.form.action.editAction" method="execute">
			<result name="success">/thankyou.jsp</result>
			<result name="input">/edit.jsp</result>
		</action>
    </package>

</struts>


Thanks a lot and SORRY :C


Re: Spring Struts problem

Posted by Lukasz Lenart <lu...@apache.org>.
2016-03-24 21:41 GMT+01:00 José Antonio Delgado Trujillo <ja...@gmail.com>:
>>
>> Can you share your config?
>
> My applicationContext.xml is…
>
> <?xml version="1.0" encoding="UTF-8"?>
> <beans xmlns="http://www.springframework.org/schema/beans"
>        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>        xsi:schemaLocation="
>             http://www.springframework.org/schema/beans
>             http://www.springframework.org/schema/beans/spring-beans.xsd">
>
>         <bean id="editService" class="pfc.struts2.form.service.EditServiceInMemory" />
>         <bean id="editAction" class="pfc.struts2.form.action.EditAction">

You must define also scope="prototype" as Struts actions cannot be singletons

>                 <property name="editService" ref="editService" />
>         </bean>
> </beans>
>
> The listener in the web.xml
> <listener>
>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>  </listener>
>
> The dependency in the POM …
>  <dependency>
>         <groupId>org.apache.struts</groupId>
>         <artifactId>struts2-spring-plugin</artifactId>
>         <version>2.3.24</version>
> </dependency>
>
> Could it be a conflict whit the version of struts2? (in this case i used 2.3.28 that it was the default of the arquetype blank).

You shouldn't mix Struts versions.

Can you share struts.xml as well?


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Spring Struts problem

Posted by José Antonio Delgado Trujillo <ja...@gmail.com>.
> 
> Can you share your config?

My applicationContext.xml is…

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="editService" class="pfc.struts2.form.service.EditServiceInMemory" />
	<bean id="editAction" class="pfc.struts2.form.action.EditAction">
		<property name="editService" ref="editService" />
	</bean>
</beans>

The listener in the web.xml
<listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

The dependency in the POM …
 <dependency>
	<groupId>org.apache.struts</groupId>
	<artifactId>struts2-spring-plugin</artifactId>
	<version>2.3.24</version>
</dependency>

Could it be a conflict whit the version of struts2? (in this case i used 2.3.28 that it was the default of the arquetype blank).

Re: Spring Struts problem

Posted by Lukasz Lenart <lu...@apache.org>.
2016-03-24 14:56 GMT+01:00 José Antonio Delgado Trujillo <ja...@gmail.com>
:

> I understood that spring can manage and inject dependency class in Action.
>

You can also use CDI instead of Spring


> In the example manage and inject EditServiceInMemory.
> To do this:
>
>    - Insert the struts spring plugin dependency in the POM.
>    - Insert the spring listener in the web.xml
>    - Write the configuration file with the bean editService.
>    - Create getter and setter to editService.
>
>
> I did it and it run.
>
> But there is an alternative: let Spring to manage also the action class
> (EditAction).
> To do this:
>
>    - Insert another bean editAction with the property editService in the
>    configuration file.
>    - Change the class in node action of struts.
>
>
> I did it and it doesn’t run.
> I don’t know why…
> The framework can’t instantiate the action class due to it doesn’t know
> who is the bean editAction.
>

Can you share your config?


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/