2

Let's assume I have Spring Rest API called application and its requests mapped on /api . It means I call for example GET method to get list of users:

localhost:8080/application/api/users

Working well. My goal is to have simple static html files alongside this API able to refer to each other. I need to find the index.html file and make it as the homepage.

localhost:8080/application/

It correctly shows me index.html using:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String homePage(ModelMap model) {
    return "home";
}

and

@Configuration
@ComponentScan(basePackages = "net.nichar.application")
@EnableWebMvc
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

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

Where I struggle is to navigate with <a href=...> over another files in the same folder index2.html, index3.html without need to explicitely write the suffix html. I try to achieve to access the webpages like

localhost:8080/application/index2

without using another @RequestMapping (except the first one mapping the home page).

One more question, is there a way to "skip" a folder in the path navigation? For clarity, I want to put these html files to webapp/static folder, however I have to access them like

localhost:8080/application/static/...

I have tried to follow a number of tutorials shortly about the Spring resources mapping, however noone of them described the solution of any similar problem. I don't use Spring Boot.

Thank you for any help.


Shortly:

How to access files in --> with:

webapp/WEB-INF/pages/index.html --> localhost:8080/application
webapp/static/index2.html       --> localhost:8080/application/index2
webapp/static/index3.html       --> localhost:8080/application/index3
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183

1 Answers1

2

You can use something like that,

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login");
    registry.addViewController("/welcome").setViewName("welcome");
    registry.addViewController("/about").setViewName("about");
    registry.addViewController("/contact").setViewName("contact");
}

where login is mapped to login.html, and welcome is mapped welcome.html. It does not require @RequestMapping, but still require an explicit mapping.

Imran
  • 1,732
  • 3
  • 21
  • 46