Reloading the page gives wrong GET request with AngularJS HTML5 mode

前端 未结 24 3292
慢半拍i
慢半拍i 2020-11-22 01:39

I want to enable HTML5 mode for my app. I have put the following code for the configuration, as shown here:

return app.config([\'$routeProvider\',\'$location         


        
24条回答
  •  日久生厌
    2020-11-22 02:10

    I had the same problem with java + angular app generated with JHipster. I solved it with Filter and list of all angular pages in properties:

    application.yml:

    angular-pages:
      - login
      - settings
    ...
    

    AngularPageReloadFilter.java

    public class AngularPageReloadFilter implements Filter {
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            request.getRequestDispatcher("index.html").forward(request, response);
        }
    }
    

    WebConfigurer.java

    private void initAngularNonRootRedirectFilter(ServletContext servletContext,
                                                  EnumSet disps) {
        log.debug("Registering angular page reload Filter");
        FilterRegistration.Dynamic angularRedirectFilter =
                servletContext.addFilter("angularPageReloadFilter",
                        new AngularPageReloadFilter());
        int index = 0;
        while (env.getProperty("angular-pages[" + index + "]") != null) {
            angularRedirectFilter.addMappingForUrlPatterns(disps, true, "/" + env.getProperty("angular-pages[" + index + "]"));
            index++;
        }
        angularRedirectFilter.setAsyncSupported(true);
    }
    

    Hope, it will be helpful for somebody.

提交回复
热议问题