【问题标题】:Spring MVC prevent url changing when returning static htmlSpring MVC在返回静态html时防止url更改
【发布时间】:2018-12-04 19:16:03
【问题描述】:

我有 Spring MVC + Angular 应用程序。 SpringMVC 唯一的“查看”任务是转发到资源/静态文件夹中的 Angular index.html 页面。

这有效,当我调用 url localhost/我的 Angular 应用程序是否出现时。 问题是浏览器中的 URL 更改为 localhost/static/static,我想阻止它。

我尝试了这里描述的许多方法,但没有任何效果。

AppController

@Controller
public class AppController {

    @GetMapping("/")
    public String test(HttpServletResponse response) throws IOException {
        return "forward:/static/index.html";
}

我尝试了添加/删除前向关键字和路径的不同组合

MvcConfiguration

@Configuration
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("static/**").addResourceLocations("static/");
    }

/*
    @Bean
    public ViewResolver getViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".html");
        return resolver;
    }
*/
/*
    @Override
    public void addViewControllers(ViewControllerRegistry registry){
        registry.addViewController("/").setViewName("forward:/static/index.html");
    }
*/
/*
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/static/");
        internalResourceViewResolver.setSuffix("*.html");
        internalResourceViewResolver.set
        return internalResourceViewResolver;
    }
*/

/*
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
    }
*/

}

代码的注释部分是我之前尝试过的方法。

Index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Ui</title>
  <base href="static/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.3ff695c00d717f2d2a11.css"></head>
<body>
  <app-root></app-root>
<script type="text/javascript" src="runtime.ec2944dd8b20ec099bf3.js"></script><script type="text/javascript" src="polyfills.66a2e3e0a1f871fa391a.js"></script><script type="text/javascript" src="main.8673ca52f204ca8863f2.js"></script></body>
</html>

感谢您的建议。

【问题讨论】:

    标签: angular spring-mvc url


    【解决方案1】:

    问题在于 resourceHandler 和设置的组合。 正确的配置是

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("static/");
    }
    

    enter <base href="./">
    

    【讨论】:

      猜你喜欢
      • 2013-06-11
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 2016-07-13
      相关资源
      最近更新 更多