1

In my Struts application, I need to create an endpoint (URL) which simply tells the user's browser to redirect to an external URL.

It looks like this can be done by creating a new Action class and defining a new <result type='redirect'> result, but that seems like overkill. I don't need an Action class to do any kind of processing on my end, just a simple redirect.

Is there a way I can set up an endpoint simply with a line or two in a configuration file?

Roman C
  • 49,761
  • 33
  • 66
  • 176
workerjoe
  • 2,421
  • 1
  • 26
  • 49
  • What do you mean by set up an endpoint? Struts configuration is used to *map* specific endpoints that is requested by the browser client and then find the corresponding action config. – Roman C Aug 02 '23 at 12:37
  • An endpoint meaning a URL defined by me. Usually I use the term "endpoint" to mean the URL without the server/domain name. So I want to know if I can add a line to `struts.xml` to map something like "/redirect_to_outside_website", not to an Action class, but directly to an outside URL. – workerjoe Aug 02 '23 at 13:12
  • If you don't want to use the existing mechanism, create one--if it's your app that's being hit it's your app that has to send the redirect. You can either do it using what's provided, or do it another way--the req wouldn't even need to get to the app. – Dave Newton Aug 02 '23 at 14:34
  • Well, that's my question: is there something that's provided? Some kind of shortcut notation that can go in struts.xml for a redirect? – workerjoe Aug 02 '23 at 14:59
  • @workerjoe Without a server/domain name your URI doesn't be an *endpoint*. Also Struts `redirect` result alows you to use internal and external URLs as well. If you made a dig into Struts configuration a bit then you could find an information how endpoints are created and served by the config-browser plugin. However the result config is a placeholder for any endpoint, but you can't execute it directly, i.e. without the action invocation. It's not clear where do you want to add a `redirect` result in `struts.xml`. You should add it to the question. – Roman C Aug 02 '23 at 16:04
  • If I knew the syntax, I wouldn't have asked the question! – workerjoe Aug 02 '23 at 16:21

1 Answers1

1

You can create the endpoint using an action configuration and without the Action class. Then add a redirect result type. You can use it with any package that extends a struts-default.

<package name="default" namespace="/" extends="struts-default">
  <action name="redirect_to_outside_website/*">
        <result type="redirect">{1}</result>
    </action>
</package>

You can see more details here.


To use slashes in action name you should use these constants

<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>


The example above can redirect to any URL using a wildcard mapper (which is a default mapper in Struts 2). If you need to fix the URL in the configuration file, then you can place a fixed URL directly to the <result> tag.

<package name="default" namespace="/" extends="struts-default">
  <action name="redirect_to_outside_website">
        <result type="redirect">https://server_domain/my_fixed_location</result>
    </action>
</package>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Yes, this works. I discovered that I can drop the `/*` and hard-code a destination in the `result` element (where you have `{1}`) if I want to simply redirect to a fixed URL. Do you want to add that to the answer? – workerjoe Aug 03 '23 at 13:59
  • 1
    Sure, just added. – Roman C Aug 03 '23 at 14:44