3

I am creating a project using Struts2.

I am setting the global-results tag in struts.xml in the following way:

<global-results>
    <result name="LOGIN" type="redirect">https://www.example.com</result>
</global-results>

I want to know if I can read the url from any other property file, so that if in future, the url gets changed, I only have to change in the property file. If I can, then how can I achieve this? Is there something like PropertyPlaceholderConfigurer class of spring framework in struts2?

pal sarkar
  • 105
  • 1
  • 9
  • 1
    `struts.xml` is your configuration. Why use a configuration to configure a configuration? – BetaRide Jan 16 '15 at 12:29
  • 1
    What difference does it make? Basically `struts.xml` is just *any other property file*. – Aleksandr M Jan 16 '15 at 12:29
  • I have to maintain three versions of code in the repository because of this url. If I can read the url from a property file, then I only have to maintain one copy of the project . It is basically for testing purposes in different environment. – pal sarkar Jan 16 '15 at 12:32
  • Aren't you *maintaining* property files also? – Aleksandr M Jan 16 '15 at 12:37
  • Why do you think Struts configuration can use properties files as a placeholder for variables in `struts.xml`? – Roman C Jan 16 '15 at 18:20
  • Thank you all for replying.I have 3 copies of the entire projects. I want to maintain only one copy. If I manage to change the link in the global tag by reading from some properties files, I think I can achieve that. If I can not do that, Please tell me if there is any other way at all? – pal sarkar Jan 19 '15 at 07:12
  • It would be really helpful If I can get a definite answer on this. Thanks! – pal sarkar Feb 10 '15 at 11:50
  • Maybe better to copy three versions of `struts.xml` and put them in three folders, named separately for dev, test and produc.... so it becomes some copy-paste work... – WesternGun Feb 23 '16 at 12:08

1 Answers1

0

I figured out one way to achieve this: with Struts alone you cannot read properties name-value pair, but via Spring and action class you can.

With Spring PropertySourcesPlaceholderConfigurer we can read value from .properties files with ${name.in.prop.file}, so we can add parameters when constructing Action bean, like this:

<bean id="someAction" class="path.to.someAction" scope="session">
    <property name="manager1" ref="manager1" />
    <property name="manager2" ref="manager2" />
    <property name="myWebsiteURL" value="${my.website.url}" />
</bean>

And, define this myWebsiteURL of type java.lang.String in Action class, along with its getter/setter, so Spring can inject the value you need in myWebsiteURL upon Action class's creation.

At last, invoke this value like this in struts.xml with ${myWebsiteURL}(the reason why it's working is still to be investigated, but it's working). For example, you can write this:

<action name="goToSomeSite" method="goToSomeSite" class="myAction">
     <result name="success" type="redirect">${myWebsiteURL}</result>
     ....
</action>

The only thing that I don't understand, is how ${myWebsiteURL} is get evaluated. With Spring? Struts? JSTL? (I don't include JSTL libs in my project)

WesternGun
  • 11,303
  • 6
  • 88
  • 157