Spring Boot 2 index.html不会从映射为静态资源的子目录中自动加载

Spring Boot 2 index.html不会从映射为静态资源的子目录中自动加载,第1张

Spring Boot 2 index.html不会从映射为静态资源的子目录中自动加载

您并不需要所有这些注释才能使其正常工作。我建议您删除那些不是您故意添加的注释。

为了在与主上下文不同的路径上提供静态页面,这里是一种解决方法。

创建另一个简单的控制器类,如下所示。

import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class Home {    @RequestMapping(path = "/")    public String getHome(){        return "redirect:/admin/ui/";       // make sure no space between colon (:) and endpoint name (/admin/ui)    }    @RequestMapping(path = "/admin/ui/" )    public  String getAdminUi(){        return "/index.html";      // your index.html built by angular should be in resources/static folder      // if it is in resources/static/dist/index.html,      // change the return statement to "/dist/index.html"    }}

并且,请注意,我已经将该类标记

@Controller
不是,
@RestController
因此如果您将其标记为
@RestController
或尝试在任何现有的类中进行相同的标记,
@RestController
您将很难轻松实现。因此,创建上面的另一个类没有害处。

这种方法的好处是,它不会破坏您现有的映射。上下文上下文路径也不会更改,因此无需理会其他端点路径。他们都应该像以前一样工作。

希望这有所帮助!



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5641563.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存