我使用 JSP 作为 View 技术开发 Spring Boot 1.3.3.RELEASE。
JSP页面、CSS、JS和图像等静态资源正在正确加载。但是如何提供 txt 或 xml(robots.txt、sitemap.xml)等静态资源
我的 Controller 正在处理请求并尝试呈现 jsp View 。
应用程序.java
@SpringBootApplication
public class SampleWebJspApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleWebJspApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleWebJspApplication.class, args);
}
}
Controller
@Controller
public class WelcomeController {
@RequestMapping(value = "/{name}")
public String welcome(@PathVariable String name) {
return name;
}
}
应用程序属性
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
以下 URL 由 Controller 处理并呈现 home.jsp
/home
/home.css
/home.js
/home.txt
/home.xml
以下网址无效
/home.jsp - 404
/robots.txt - 404 - trying to render robots.jsp
/sitemap.xml - 404 - trying to render sitemap.jsp
最佳答案
Spring-Boot 不再做 jsp 了,他们试图强制你使用 thymeleaf 或其他模板引擎,静态资源可以从某些目录中获得。/static 就是其中之一。并且 thymeleaf 文件需要位于 templates 文件夹中。
我在最新的 Spring Boot 上的设置如下
application/src/main/resources/static
/templates application.properties
对于其他位置,您需要为其他位置/robots.txt 等添加资源处理程序
关于java - Spring Boot 服务静态资源 - sitemap.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40150195/