1

Using below struts.xml setting, changing action extension from .action to .html was a success.

<constant name="struts.action.extension" value="html"/>

However, the old links from Google search results or other external links are still pointed to .action url, it always redirect to no page found error when clicked. Is there anyway I can redirect those .action url to latest .html links?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
elfwine
  • 41
  • 3
  • You can put both in the configuration. And see this [Struts2 .action extension causing CSS, JavaScript and Struts Dojo to break](http://stackoverflow.com/q/12607075/1700321). – Aleksandr M Mar 21 '17 at 07:13
  • Thank you for your comment. I resolved it now by adding an Interceptor that will replace the extension .action to .html and redirect it. – elfwine Mar 21 '17 at 09:39
  • @elfwine Can you add a code for the interceptor? – Roman C Mar 21 '17 at 12:29
  • @RomanC Sorry for late response, I will post the code at the answer section below. – elfwine Mar 31 '17 at 08:28

1 Answers1

0

Here is the code for the interceptor that replaces .action url to .html. Don't forget to declare the added interceptor in struts.xml. I hope it will be helpful to others looking for it. ;)

@Override
public String intercept(ActionInvocation actioninvocation) throws Exception {
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    StringBuffer url = request.getRequestURL();
    if(url != null && url.indexOf(".action") > 0) {
        String fullUrl = url.toString().replace(".action", ".html");
        String queryString = request.getQueryString();
        fullUrl += (queryString == null ? "" : ("?" + queryString));
        // return new url with .html
        response.sendRedirect(fullUrl);
    }
    return actioninvocation.invoke();
}
elfwine
  • 41
  • 3