Spring-boot configuration

1.6k views Asked by At

I am trying to use a yeoman angular generator inside a Spring-boot application. I have the following project structure

/project
 -/src/main
 -- /java
 -- / resources
 --- /application.properties
 --- /public
  ---- /app
  ----- /index.html
  ----- /bower-components
  ----- /scripts
  ----- /views/**.html
  ----- /images
  ----- /styles

My goal is when the application loads up, index.html page would load from /project/src/main/resources/public/app/index.html and the URL should be "localhost:8080/#/". Currently, the URL looks like this localhost:8080/app/index.html.

I tried to put the mapping in application.properties as :

server.context-path=/public/app

I also tried to overwrite the Application configuration by extending WebMvcConfigurerAdapter. The class is :

@Configuration
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/public/" };

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                CLASSPATH_RESOURCE_LOCATIONS);
    }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {        
        registry.addViewController( "/" ).setViewName( "app/index" ); 
    }
}

But looks like this isn't working either. Can anyone comment on what am I missing and how to achieve the URL as "locahost:8080/#/" even if the index.html is located at /src/main/resources/public/app.

Note: If I keep my index.html page at /src/main/resources/public, the URL is shown as desired [locahost:8080/#/] . Any help is appreciated.

Thanks

2

There are 2 answers

1
andriy On

Personally, I'm not sure if it is correct to access to your application's index page via localhost:8080/#/. As '#' is used to navigation on current page. Take a look at enter link description here answer about hyperlinks.

Edited:

Looks like your problem in CLASSPATH_RESOURCE_LOCATIONS. Have you tried to change "classpath:/resources/" to "classpath:/resources/app/"?

Otherwise, you can move your html files from resources folder to WEB-INF and add the configuration of InternalResourceViewResolver to your @ApplicationConfiguration class, set it as following:

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

I hope this helps.

0
Anirban On

In my application configuration, I added the app folder

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/public/app/" };

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

Then added the method addViewControllers as :

@Override
    public void addViewControllers(ViewControllerRegistry registry) { 
        registry.addViewController("/").setViewName("forward:/index.html") ;
    }

No change in my folder structure (as mentioned the question) was required.

Hope this helps others as well.