You can if you want to use a String as a Spring bean, it's a different issue as to whether or not it is considered best practice or whether there are alternatives. You can find examples on the SpringSource forum if you want, commented upon by Springy folk. It is perfectly valid to do so..

Sorry to sound off here but could input to the topic be constrained to why it might be that Struts somehow messes up the redirect by picking this bean up even though there are no references to it an any of the struts action beans or in the struts.xml.. I think we can infer from this that if any context file, regardless of purpose (related to Struts, data access or any other whatever) declared a String as a bean this will mess struts redirects up.. which is something that shouldn't happen.



On 09/05/2011 11:16, Ilya Kazakevich wrote:
Not sure you can use primitive wrappers and strings as spring beans. For
example autowiring does not work for them.
Try the following:

1) inject literal directly
Instead of
<property ref="myString"
Try
<property value="myStringContent"

2) Create your own wrapper for string like Configuration class with method
getMyProperty


Ilya Kazakevich,
Developer
JetBrains Inc
http://www.jetbrains.com
"Develop with pleasure!"

-----Original Message-----
From: Marcus [mailto:mar...@marcusbond.co.uk]
Sent: Monday, May 09, 2011 11:27 AM
To: user@struts.apache.org
Subject: Re: Strange behaviour with Spring

Not sure what you mean, the bean I'm referring to is a Spring bean not a
JavaBean (two unrelated concepts).. there is no requirement when using
the Spring framework to not create Spring beans of type String.

Regardless, this doesn't help explain why the String is being picked up
by the Struts framework and set on the redirect when there is nothing in
the configuration to indicate it should..

On 09/05/2011 07:13, Chris Pratt wrote:
You can't create beans that implement java.lang.String, it's immutable and
doesn't follow the bean spec.
    (*Chris*)

On Sun, May 8, 2011 at 2:11 PM, Marcus Bond<mar...@marcusbond.co.uk>
wrote:
Hi,

I'm using Struts 2.2.3 and have observed some bizarre behavior when using
Spring to provide my actions.. In order to simplify this and prove the
problem I have created a project with a single static html file, and a
single action in struts.xml which references a bean in my spring context
file.

What basically happens is that if I declare a bean in my spring context
(bean unrelated toany struts action) that is of class java.lang.String
(as I
do for a log4j filepath) then when I perform a redirect in an action
mapping
this gets appended to the end of the url as the anchor.

If I navigate to http://localhost:8080/My App/simpleRedirect
the result is a reirect to the 'simple' action but note the url struts
has
redirected to:


http://localhost:8080/MyApp/simple#this%20is%20a%20java.lang.String%20bean%2
0in%20my%20spring%20context
For some reason the org.apache.struts2.dispatcher.ServletRedirectResult
class is being instantiated by with the anchor and location constructor
args
set to the value of my spring bean by the StrutsSpringObjectFactory

If I remove the String bean from the spring contect then the url behaves
as
normal, redirecting to:
http://localhost:8080/MyApp/simple

Have been scratching my head trying to find where this behaviour is
happening but have run out of time.. is this a known issue?

Files content:
*Spring context file:*

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

     <!-- Expose String as a bean (usually for a file path)-->
     <!-- Picked up by Struts and set as location and anchor on a redirect
and presented in url
         If commented out this does not happen -->
     <bean id="someFileName" class="java.lang.String">
         <constructor-arg value="this is a java.lang.String bean in my
spring
context" />
     </bean>

     <!--  action beans -->
     <bean name ="simpleAction"
class="uk.co.marcusbond.action.SimpleAction"
scope="prototype" />
</beans>

*Struts file:*
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     "http://struts.apache.org/dtds/struts-2.0.dtd";>
<struts>
     <package name="simple" extends="struts-default">
         <!-- returns success and shows index.html -->
         <action name="simple"
             class="simpleAction">
             <result>index.html</result>
         </action>

         <!-- redirects through the 'simple' action -->
         <action name="simpleRedirect">
                 <result name="success" type="redirect">simple</result>
         </action>
     </package>
</struts>

*Java class uk.co.marcusbond.action.SimpleAction*
package uk.co.marcusbond.action;

public class SimpleAction  {

     public String execute() {
         return "success";
     }
}




__________ NOD32 6105 (20110508) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com


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


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



__________ NOD32 6106 (20110509) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com





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

Reply via email to