简体   繁体   English

角html5mode服务器重写Wite Spring MVC

[英]Angular html5mode server rewrite wite spring mvc

Can someone guide me on server rewrite in (AngularJS+spring mvc+tomcat server) app. 有人可以指导我在(AngularJS + spring mvc + tomcat服务器)应用中重写服务器。

Till now I did following settings: 到目前为止,我已经进行了以下设置:

<base href="/Eatery/index.html">
$locationProvider.html5Mode(true);

I don't have web.xml 我没有web.xml

I am using java configuration in spring mvc 我在Spring MVC中使用Java配置

Based on the answers from How to use a servlet filter in Java to change an incoming servlet request url? 基于“ 如何在Java中使用servlet过滤器来更改传入的servlet请求url”中的答案 I ended up with a Spring OncePerRequestFilter . 我最后得到一个Spring OncePerRequestFilter

It checks the incoming URLs whether they are for static resources (by extension) or whether they are calls to the REST api (with /api/ substring) and allows them to be executed as usual. 它检查传入的URL是用于静态资源(通过扩展名)还是用于REST api的调用(带有/api/子字符串),并允许它们照常执行。 In other cases, it redirects to the /index.html so Angular's router can handle the request. 在其他情况下,它重定向到/index.html因此Angular的路由器可以处理该请求。

public class Html5ModeUrlSupportFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain)
                                    throws ServletException, IOException {
        if (isStatic(request) || isApi(request)) {
            filterChain.doFilter(request, response);
        } else {
            request.getRequestDispatcher("/index.html").forward(request, response);
        }
    }

    private boolean isApi(HttpServletRequest request) {
        return request.getRequestURI().indexOf("/api/") >= 0;
    }

    private boolean isStatic(HttpServletRequest request) {
        return Pattern.matches(".+\\.((html)|(css)|(js))$", request.getRequestURI());
    }
}

Of course, the rules of detecting requests must be adjusted to your needs. 当然,必须根据您的需求调整检测请求的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM