IF 请求映射未解析 AND 路径未在静态资源位置中找到,然后重定向到 index.html [英] IF request mapping unresolved AND path not found in static resources location then redirect to index.html

查看:16
本文介绍了IF 请求映射未解析 AND 路径未在静态资源位置中找到,然后重定向到 index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题(由前端、angularjs 上下文引起):

"X 在其位置服务中使用 HTML5 模式.这意味着,您将看到类似 www.example.com/mail 的 URL,而不是这样的 URL:www.example.com/#/mail

"X uses HTML5 mode in its location service. It means, you will see URLs like www.example.com/mail rather than seeing URLs like this one: www.example.com/#/mail

此外,如果不遵循 .htaccess 规则,您将无法直接访问您的应用程序.因此,在 URL 栏中输入 www.example.com/mail 或刷新您的应用程序将导致内部服务器错误.

Also, without following .htaccess rules, you can't access your application directly. So typing www.example.com/mail to the url bar or refreshing your application will cause an Internal Server Error.

创建一个与 index.html 位于同一目录的 .htaccess 并将以下代码粘贴到其中:"

Create an .htaccess that stays with the same directory as your index.html and paste the following code inside it:"

 <IfModule mod_rewrite.c>
        RewriteEngine On
        # Send all requests to the index.html unless
        # it's a directory or a file that actually exists
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.html [L]
 </IfModule>

当我使用 tomcat 时,这个脚本与必须放在 Spring Boot 应用程序中的伪代码一样有用.否则浏览器中直接输入的路径都不起作用(获取 404 白标服务器页面).

As I am using tomcat this script is as as useful as pseudo code which must be put in Spring Boot application. Otherwise none of the directly typed paths within browser work(get 404 white label server page).

实际上我需要实现类似的东西.我什至不知道我应该查看哪些组件.

Effectively I need to implement something similar. I can't even figure out which components I should even look at.

到目前为止,我脑子里的信息是杂乱无章的:

So far disjointed pieces of info in my brain:

  • 过滤器链和资源处理程序中的 Spring 过滤器按我提到的顺序命中(根据跟踪日志).
  • 每当路径为:A. 不是 rest api 控制器路径 ANDB. 未解析为静态资源(在我的情况下:静态"资源文件夹).

那么我需要创建自定义资源处理程序/自定义过滤器吗?或者问题出在不同的维度?(tomcat本身?)

So do I need to create custom resource handler/custom filter? Or maybe problem lies in different dimensions? (tomcat itself?)

推荐答案

问题比技术更符合逻辑:

The problem was more logical than technical:

A.必须由angular js解析的路径与backed无关-> 意思是,后端不知道 angular 知道哪些路径,哪些不知道.

A. The paths which must resolved by angular js have nothing to do with backed -> meaning, that backend has no knowledge which paths angular is aware of and which are not.

B.因此,如果控制器和资源处理程序未解析资源 - 这是后端的 404 情况.所以:

B. Therefore, if a resource is not resolved by the controllers and resource handlers - this is 404 case for backend. So:

@Component
public class WebConfiguration implements EmbeddedServletContainerCustomizer {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
        configurableEmbeddedServletContainer.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/index.html"));
    }
}

解决问题.结果是,每当我们转到 /mail 时,后端返回 index.html ,然后 angular 解析内部路径并显示电子邮件部分.它的角度如何我自己也不确定,我不知道 html5 模型是如何记住路径的.

Resolves the issue. The outcome is that whenever we go to /mail, backend returns index.html , then angular resolves the internal path and displays email partial. How angular does it I am not sure myself, I don't know how the html5 model remembers the path.

这个方案的不足是请求rest api,定位错误的rest api路径会收到index.html,但是status设置好了应该没什么大不了的.

The shortage of this solution is that request to rest api, targeting wrong rest api path will receive index.html, but status is set so should be no big deal.

但是很高兴知道在后端流中的哪个点知道请求既没有解析为控制器也没有解析为静态资源以及如何捕获这种行为.

这篇关于IF 请求映射未解析 AND 路径未在静态资源位置中找到,然后重定向到 index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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