0

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
Mukun
  • 1,756
  • 10
  • 30
  • 57
  • You have designed your controller to handle those URLs and now you are surprised that it behaves the way you designed it to behave? – a better oliver Oct 20 '16 at 13:04

3 Answers3

1

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

Community
  • 1
  • 1
Theresa Forster
  • 1,914
  • 3
  • 19
  • 35
  • I have robotx.txt and sitemap.xml under static folder. But still it tries to load *.jsp . But strange it does not handle *.jsp – Mukun Oct 20 '16 at 09:22
  • If you can contact me on github i can let you have access to my working repo and you can see all the config without sharing it over the internets to the world + dog, github name is theresajayne – Theresa Forster Oct 20 '16 at 09:24
  • Is it possible to message a user in Git. ? I don't see any option. apart from following and creating issue and pull request. – Mukun Oct 20 '16 at 09:47
  • well i use theresajayne everywhere else, discord, facebook etc. my gmail is tjforste70@gmail.com, are you Mugundan? – Theresa Forster Oct 20 '16 at 09:50
0

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>
AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • I already have those dependency and JSP is working. problem in serving static resource like text file or xml file . – Mukun Oct 20 '16 at 09:43
  • Then, Theresa might be right about some limitations . See the official note about restrictions. it says: JSPs should be avoided if possible, there are several known limitations when using them with embedded servlet containers. http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-jsp-limitations . It doesnt states anything about static resources of type txt or xml. – AchillesVan Oct 20 '16 at 09:56
0

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

Community
  • 1
  • 1
AntJavaDev
  • 1,204
  • 1
  • 18
  • 24