3

I'm working on an webapp where the user selects an account to work with, then all URLs they click thereafter refer to that account. Currently the active account is stored in the session, but that means it's not included in user bookmarks, it's lost when the user's session times out, and when the user shares links they don't include the relevant account...

Is there any way I can include the accountID in every URL without adding it manually to all URLs in the (rather large & still growing) webapp?

One approach would be to make it the first part of the path, so all URLs would be something like:

  • /123/discussions/view.do
  • /123/account/edit.do

and so on, where 123 is the accountID.

If all paths could be relative, that'd work just fine. But I haven't figured out how to get this kind of approach to play nicely with Struts namespaces.

Normally, all paths generated by the Struts2 taglibs start with /, and action wildcard matches also doesn't kick in until after the namespace is matched.

If I can somehow get the accountID in the path after the namespace part, I can hit the right action with action wildcards, I think, but the hard part is still getting the ID into the URLs in the first place.

The best solution may just be making custom versions of <s:a>, <s:form>, and <s:url>, but I'm not sure how easy that is.

IMPORTANT

There are a few answers that address only how to map these URLs to Actions.

The harder part of this problem is actually how to get the accountID into all URLs and form paths without manually adding it to each one throughout the application.

Currently it seems to me that the only way will be to make custom versions of Struts JSP tags for URLs and forms. So any pointers on how to do that would be useful.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Rob Whelan
  • 1,281
  • 13
  • 15

2 Answers2

0

You should consider the choice of using Parameters in namespaces of wildcard mappings.

From Struts 2.1+ namespace patterns can be extracted as request parameters and bound to the action. To enable this feature, set the following constant in struts.xml:

<constant name="struts.patternMatcher" value="namedVariable"/>

With that in place, namespace definitions can contain {PARAM_NAME} patterns which will be evaluated against the request URL and extracted as parameters, for example:

@Namespace{"/users/{userID}");
public class DetailsAction exends ActionSupport {
 private Long userID;
 public void setUserID(Long userID) {...}
}

If you are running Struts2 version prior 2.1+, you need to upgrade to the latest version.

As a workaround you can do this with UrlRewriteFilter.

You can also check this answer for code examples.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • We have a large application with actions in various namespaces. Wildcards (and advanced wildcards) seem to let me add variables to the action only, not the namespace. Setting struts.patternMatcher to "namedVariable" would let me insert variables into the namespace... but the docs warn me that some tags "may write the literal namespace into the HTML (eg /{user}/2w) instead of the path used in the request (ie. /brett/24)" -- this is the part I'd really like to work, though... Their solution: "This problem can be avoided by using HTML tags directly with relative paths or explicit URLs." Hmm. – Rob Whelan Oct 07 '14 at 02:15
  • Do you have any problems with tags? Generally I would avoid such solution if it doesn't work 100%. `namedVariable` matcher was introduced in 2.1.7 and lately in 2.1.9 added a new `regex` matcher which is more powerful. – Roman C Oct 07 '14 at 10:30
0

check out Advanced Wildcard Mapping

You should define your action like this: /{accountId}/discussion/view.do/
Here is an Example for this: Example

Community
  • 1
  • 1
Jaiwo99
  • 9,687
  • 3
  • 36
  • 53