0

I am a newbie to spring boot architecture. It says that to let the index pages to find static resources like js, we need to keep it under "src/main/resources/static".

Directory Structure:

Html files: src/main/webapp/WEB-INF/jsp/

js files: src/main/resources/static/js/

This is my index page:

<html ng-app="RollbackApp">

<head>
    <title>My Rollback View</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.js"></script>
    <script type="text/javascript" src = "js/app.js"></script>
</head>

<body>
<div ng-controller="rollbackController"><p>
    <button ng-click="rollback()">RollBack</button>
</p></div>

</body>
</html>

Currently the index page is not able to load my "app.js"

My Mvc config class is as follows:

package com.manoj;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * Created by manojma on 10/13/2017.
 */
@Configuration
@EnableWebMvc
public class ApplicationWebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

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

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".html");
        return resolver;
    }

}

I am unable to find the reason, why it is not able to find my js files.

Please help me with this.!!

Manoj Majumdar
  • 505
  • 1
  • 4
  • 23
  • Unrelated, but why do you create your own `ApplicationWebMvcConfig` if Spring boot already has one? If you want to customize the prefix/suffix for those JSPs, you can use the `spring.mvc.view.prefix` and `spring.mvc.view.suffix` properties. And even more questionable is why you're using JSPs when you're not using any JSP related stuff and even named the suffix `*.html`. – g00glen00b Oct 13 '17 at 11:19
  • Adding spring.mvc.view.prefix in application.properties is not solving my problem. .So I searched around and found this solution and this is perfectly running for me.. But the problem is it is not able to find js files.. Is there any method in WebMvcConfigurerAdapter to add resources like js files. – Manoj Majumdar Oct 13 '17 at 11:33
  • Regarding the question of "Why I am using Jsps", there is a requirement to use spring boot and I am finding a way to use it with angularjs as the view of application is rendered using angularjs – Manoj Majumdar Oct 13 '17 at 11:34
  • That's exactly why I was asking, you can also put your `index.html` in the `src/main/resources/static` folder as well and it will be served like JavaScript. Unless you're going to use serverside rendering (you're using AngularJS, so probably not), you don't actually need JSP and could remove that complexity entirely from your application. – g00glen00b Oct 13 '17 at 11:59
  • I need to set index.html as the starting page and keeping index.html in resources/static folder in such case, is not helping. – Manoj Majumdar Oct 13 '17 at 12:14
  • `index.html` is already the default welcome page within Spring boot. But you can also configure this, as you can see in https://stackoverflow.com/questions/27381781/java-spring-boot-how-to-map-my-app-root-to-index-html. – g00glen00b Oct 13 '17 at 12:34

2 Answers2

1

The issue is that your resource handler isn't configured correctly. You've set /resources/static/ as your resource location. However, considering that src/main/resources is put entirely on your classpath, you should leave away the /resources part. Additionally, you should mention that you're looking on your classpath, so you should probably use classpath:/static/.

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

Additionally to that, you've defined the resource handler to forward requests starting from /resources/**. That means that if you relatively request js/app.js, it won't work, since it won't trigger the resource handler. You need to use resources/js/app.js:

 <script type="text/javascript" src="resources/js/app.js"></script>

However, if your goal is to just statically serve some HTML pages, it's a lot easier to just put the HTML pages into the src/main/resources/static folder as well. Spring boot already serves the index.html by default as the welcome page but it can be customized.

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
0

you have to be careful of overriding any of the defaults

I struggled with connecting my static resources to my jsp pages and this is what I finally used to get it working with Spring boot 2.0. You can see my properties and also what the urls look like when mapping to static resources like images or plain html.

Next we need to define the template prefix and suffix for our JSP files in application.properties. Thus add: (the context path 'pdx' is optional and you can pick a name to match your application)

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.servlet.context-path=/pdx

http://localhost:8080/pdx/images/thedocks.jpg access static resources in src/main/resources/static/images/thedocks.jpg

http://localhost:8080/pdx/ loads index.html in src/main/resources/static/index.html

http://localhost:8080/pdx/css/home.css loads css class in src/main/resources/static/css/home.css

http://localhost:8080/pdx/h loads my home controller with @Controller("/") and @GetRequest(“/h”) annotations.

my jsp page loads the static image like this

<img alt="the docks" src="/pdx/images/thedocks.jpg"/>
JesseBoyd
  • 1,022
  • 1
  • 13
  • 18