javaspringjspspring-mvcspring-boot

Fastest way to deliver jsp views without xml-conf using spring project stub from start.spring.io


I am using spring again after a longer period and happy to see that xml configurations are no longer required in every case.

I want to build a RESTful App, but I still have to deliver the frontend app. I figured the simplest way without using any additional template engines like thymeleaf would be serving a static jsp.

I'm using the project template from start.spring.io with just spring-mvc as dependency, thus I'm using spring boot as well.

I wrote a controller in order to deliver the jsp, but it seems that the mapping for the views has to be configured first.

@Controller
public class StaticPagesController {

  @RequestMapping(value = "/")
  public String index(){
    return "index";
  }
}

So I created a configuration class:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "de.tuberlin.sense.emp")
public class WebConfiguration {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".html");

        return viewResolver;
    }
}

index.html is located in main/webapp/WEB-INF/views/

When I send a request to /, I get a WARN in the logs which states:

No mapping found for HTTP request with URI [/WEB-INF/views/index.html] in DispatcherServlet with name 'dispatcherServlet'

What am I missing? Can I do this without any xml configuration?

Here is my main application class code:

UPDATE:

@SpringBootApplication
public class ExperimentManagementPlatformApplication {

        public static void main(String[] args) {
            SpringApplication.run(ExperimentManagementPlatformApplication.class, args);
    }
}

Solution

  • I was able to serve the static page without needing any controller at all by just adding the html file to the static directory. Furthermore i found out that the entire webapp directory is not included when the app is deployed as jar which explains why the files in there could not be found.

    The Spring documentation