简体   繁体   中英

Spring boot serving static resource - sitemap.xml

I work on spring boot 1.3.3.RELEASE with JSP as view technology.

JSP pages , static resources like CSS, JS and images are loading properly. But how to serve static resource like txt or xml (robots.txt, sitemap.xml)

My controller is handling the request and trying to render jsp view.

Application.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;
    }
}

application.properties

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

Following URL's handled by controller and it renders home.jsp

/home  
/home.css
/home.js
/home.txt 
/home.xml 

Following URL's Not working

/home.jsp - 404 
/robots.txt - 404 - trying to render robots.jsp
/sitemap.xml - 404 - trying to render sitemap.jsp

Spring-Boot doesnt do jsp's anymore, they are trying to force you to use thymeleaf or another templating engine, static resources are available from certain directories. /static is one of them. and the thymeleaf files need to be in a templates folder.

My setup on my latest spring boot is as follows

application/src/main/resources/static

  /templates application.properties 

for other ones you need to add a resourcehandler for the other locations /robots.txt etc

Jsp still works with spring boot.

Not sure if you already did this but its important that you add these dependencies to your maven or gradle.

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

You have configured the Spring's View Resolver with this line spring.mvc.view.prefix , so every response returned by your controllers , will be chained to the view Resolver , which will try to find the resource under /WEB_INF/JSP based on the string name you returned(not sure if you have placed this folder under resources , as your app is a spring boot one , not a java web app). In order to do that and keep the view resolver , either wire up another servlet to share static resources or wire up a ResourcesController with default locations. Something like :

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
        "classpath:/myStaticResources/", "classpath:/static/" };

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

More info here or here

Also Spring boot gives you this way as well :

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/

More info about the application properties here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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