简体   繁体   English

如何使用ResourceHandlerRegistry和Spring MVC映射单个文件?

[英]How do I map a single file using ResourceHandlerRegistry and Spring MVC?

I have a Spring MVC app where I define a static resource haandler like this: 我有一个Spring MVC应用程序,其中我定义了一个静态资源处理程序,如下所示:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (resourceIdentifier != null) {
        registry.addResourceHandler("/admin-ui/**", "/admin-ui/").addResourceLocations(
                "file:" + resourceIdentifier.getResourcesLocation() + "/");
    }
}

(For brevity, I have elided the rest of the HandlerInterceptor declaration.) (为简便起见,我省略了HandlerInterceptor声明的其余部分。)

Now, the main entry point of the app is: 现在,该应用程序的主要入口是:

https://foo.mysite.com/admin-ui/index.html

Instead, I want the entry point to be: 相反,我希望入口点是:

https://foo.mysite.com/admin-ui

Currently, this gives a 404 error. 当前,这将产生404错误。 What is the easiest way to enable this, preferably using the ResourceHandlerRegistry ? 最好使用ResourceHandlerRegistry来实现此目的的最简单方法是什么?

I added the following controller, which works partially: 我添加了以下控制器,该控制器部分起作用:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("admin-ui").setViewName("/admin-ui/index.html");
}

I needed to make a few other changes in my app since the other assets, eg, the css, js, and template files were being accessed relative the index.html (ie, with no admin-ui/ as part of the path). 由于需要相对于index.html访问其他资产(例如css,js和模板文件),因此我需要对我的应用程序进行一些其他更改(即,路径中没有admin-ui/ )。 I needed to change this to being an absolute path to the server root, so for example in index.html: 我需要将其更改为服务器根的绝对路径,例如在index.html中:

<link href="css/bootstrap.min.css" rel="stylesheet">

became this: 变成这样的:

<link href="/admin-ui/css/bootstrap.min.css" rel="stylesheet">

After doing this for all urls in the app, I can now access the app with or without the index.html . 在对应用程序中的所有网址执行完此操作之后,我现在可以访问带有或不带有index.html的应用程序。

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

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