How to do dynamic URL redirects in Struts 2?

后端 未结 6 704
悲哀的现实
悲哀的现实 2020-11-28 04:57

I\'m trying to have my Struts2 app redirect to a generated URL. In this case, I want the URL to use the current date, or a date I looked up in a database. So /section/

6条回答
  •  生来不讨喜
    2020-11-28 05:08

    I ended up subclassing Struts' ServletRedirectResult and overriding it's doExecute() method to do my logic before calling super.doExecute(). it looks like this:

    public class AppendRedirectionResult extends ServletRedirectResult {
       private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    
      @Override
      protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
        String date = df.format(new Date());
        String loc = "/section/document/"+date;
        super.doExecute(loc, invocation);
      }
    }
    

    I'm not sure if this is the best way to do it, but it works.

提交回复
热议问题