Angular HTML5 URL-服务器配置 [英] Angular HTML5 URLs - Server configuration

查看:131
本文介绍了Angular HTML5 URL-服务器配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读到AngularJS使用hashbang URL作为默认值-但这不是优点,因此应使用HTML5 URL. 为了在客户端配置此行为,必须执行以下操作:

I have read that AngularJS uses hashbang URLs as default - but thats not an advantage and therefore HTML5 URLs should be used. In order to configure this behaviour at client side the following must be done:

...
$locationProvider.html5Mode(true);
...

在服务器端也必须完成一些配置-据我了解,应该配置URL请求,因此,应将内部带有ng-app的页面(起始页面-例如index.html)传递给客户端.排除提供静态资源的URL(CSS,图像,AngularJS局部对象……)和用于CRUD的URL(例如RESTful服务URL).

At server- side also some configurations have to be done - as I understood URL requests should be configured therefore that the page with ng-app inside (the starting page - e.g. index.html) should be delivered to client. Excluded are URLs which deliver static resources (CSS, images, AngularJS partials, ...) and URLs which are used for CRUD (e.g.RESTful service URLs).

我的问题不是如何在服务器端调整此行为(例如,对于内部装有嵌入式服务器的String Boot)?

My question not would be how to adjust this behaviour at server side (e.g. for String Boot with an embedded server inside)?

另一个问题是,是否要在AngularJS应用程序中区分静态资源(例如.../static/..)和CRUD URL(例如.../database/...)?

And another question would be if you distinguish between static resources (e.g. .../static/..) and CRUD URLs (e.g. .../database/...) in our AngularJS application?

非常感谢!

推荐答案

在Spring Boot中,您可以像这样配置一个万能的视图控制器:

In Spring Boot you can configure a catch-all view controller like this:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.setOrder(Ordered.LOWEST_PRECEDENCE);
        registry.addViewController("/**").setViewName("forward:/index.html");
    }
}

这会将所有其他未处理的请求转发到index.html. setOrder(Ordered.LOWEST_PRECEDENCE)用于确保仅在没有其他路由被发现之后才应用此包罗万象的转发.

This forwards all requests not handled otherwise to index.html. setOrder(Ordered.LOWEST_PRECEDENCE) is used to ensure, that this catch-all forwarding only applies after no other route has been found.

但是,现在所有资源查找也都通过此视图控制器进行路由(因为WebMvcAutoConfiguration在应用默认资源处理程序之前会检查路径模式的存在).

However, now all resource lookups are also routed through this view controller (because WebMvcAutoConfiguration checks for the existence of the path pattern, before applying the default resource handler).

一个可能的解决方法是在application.properties中设置静态路径模式以包括对文件扩展名的检查:

One possible fix is setting the static path pattern in application.properties to include a check for a file extension:

spring.mvc.static-path-pattern = /**.*

这仅在您的角度路线不包含任何扩展名的情况下有效. 另一种方法是完全禁用Spring Boot的资源处理(spring.resources.addMappings = false)并替换您自己的资源.

This only works if your angular routes don't contain any extensions. Another way is to completely disable spring boot's resource handling (spring.resources.addMappings = false) and substituting your own.

这篇关于Angular HTML5 URL-服务器配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆