Redirect to another action in an interceptor in struts 2

后端 未结 3 1862
花落未央
花落未央 2021-02-03 11:38

I am currently in the process of learning Struts 2 and I am currently building a simple application where unverified users are redirected to a login form.

I have a login

3条回答
  •  遥遥无期
    2021-02-03 12:08

    If you need to use send redirect, return null to avoid this problem (example redirecting from www.domain.com to domain.com):

    public String intercept(final ActionInvocation invocation) throws Exception {
    
        String url=RequestUtil.getURLWithParams();  //you should implement this
        int index=url.indexOf("www");
        if (index!=-1 && index<10) {
            //Note: <10 to check that the www is in the domain main url
            //https://localhost:8443/mycontext/myaction.action?oneparam=http://www.youtube.com/user/someuser
            String redirection=url.replaceFirst("www\\.", ""); 
            LOG.debug("Redirection from "+url+" to "+redirection);
            RequestUtil.getResponse().setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            RequestUtil.getResponse().sendRedirect(redirection);
            return null;
        }
        return invocation.invoke();
    }
    

提交回复
热议问题