java - Spring Boot 服务静态资源 - sitemap.xml

标签 java spring jsp spring-mvc spring-boot

我使用 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/

相关文章:

java - 如何在 JNI 中将事件/信号从 C 发送到 Java

java - 通过AOP拦截重载方法

java - 通过 jpa 将两个表连接到一个 java 对象中

javascript - 如何在没有javascript的情况下以jsp形式验证客户端的日期?

java - 如何使用表达式语言迭代具有值 Map<String,Integer> 声明为 Map<String,Map<String,Integer>> 的映射?

java - 如何获取 Spring Batch Integration 测试来加载我的应用程序属性?

java - 如何提高JPictureBox大图像的绘制速度?

java - Spring Boot 应用程序无法启动,因为类 org.eclipse.jetty.server.Server 不存在

java - Hibernate JPA 拦截器没有得到调用

java - 是否可以选择在 onSubmit 中发送的表单上的项目?