user3386275
user3386275

Reputation: 65

Spring MVC cannot find *.jsp files

I am an absolute noob when it comes to web development. But I got some background in C/C++/Java so I don't have a problem with MVC controllers. It's the configuration that is giving me the headache.

I am using Spring Boot. And according to the tutorials it can magically resolve everything without even opening an editor and typing a single character. Apparently not.

I have a view resolver configure as such:

@Configuration
@ComponentScan (basePackages = {"my.test.controller"})
@EnableAutoConfiguration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
  }

@Bean
    public InternalResourceViewResolver getViewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
   }



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

}

I have a controller like this:

@Controller
public class PageController {

@RequestMapping(value = "/index")
public String doSomething() {

        //.. do Something

        return "/index";
    }

My main problem is it cannot find the file if there is a jsp extension in the address. If I type the url without an extension like localhost:8080/index the page is displayed properly. If I type the url with an extension like localhost:8080/index.jsp the page returns a 404 error. This is the same for all pages declared in the controller.

Any help will be greatly appreciated. Thanks thanks.

Upvotes: 3

Views: 10921

Answers (4)

tyrantqiao
tyrantqiao

Reputation: 339

Well, I am not sure this answer will help you, as the question was posted in 2014. In order to help people resolve this question, I provide some of my resolutions. Hope this will help.

  • make sure your @Controller 's configuration @RequestMapping("/xx") cannot be the same as your view (jsp or templates)

    For example, you have a view named home.html. You cannot let the @RequestMapping() be the same as the view's name. This will cause a circular error (--> Circular view path, added below). How to fix this error, The path cannot be the name. (That's the JSP files mostly happen)

    When you input the same name, you will get this:

    Circular view path [preference]: would dispatch back to the current handler URL [/preference] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

    Here is a link that explains why this error would happen.

  • This kind of error is only for the HTML5 file. When you get some wite page error and you are using the HTML5 file and cannot find other errors, that might be this one below When you create an HTML file, the basic file will be the below code.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
    </body>
    </html>
    

    The <meta charset="UTF-8"> didn't end with /> or </meta>. When in the HTML5 file, that would be right. But Thymeleaf uses XHTML to load the file, so the <meta> should be closed.

Upvotes: 1

FriendlyMikhail
FriendlyMikhail

Reputation: 2927

I remember having the same problem when I started with spring, the "url" that you use needs to correspond to a particular Request Mapping, not necessarily a particular page for example

@RequestMapping(value = "/home")
public String doSomething() {

    //.. do Something

    return "/index";
}

will expose an endpoint at localhost:8080/home not localhost:8080/index or localhost:8080/index.jsp

A great example project is located at: https://github.com/mariuszs/spring-boot-web-jsp-example

Upvotes: 1

RPaul
RPaul

Reputation: 946

If you mapped all of your requests to dispatcher servlet from web.xml,then it will check for the appropriate controller mappings.

You have mapped the request to /index so it can't process a /index.jsp

then internal view resolver will return the view just like you configured.

you can try

@RequestMapping(value = {"/index","/index.jsp"})

It is better to avoid .jsp extension in a web app.

Upvotes: -1

Dave Syer
Dave Syer

Reputation: 58094

There's a JSP sample in Spring Boot that you can crib from. If I were you I wouldn't define a ViewResolver since Boot already does that for you (but if you want to use prefix and suffix resolution you need to set spring.view.prefix and spring.view.suffix).

Your @Controller should return view names (not paths), so "index" is going to be resolved as "/WEB-INF/views/index.jsp" with your existing setup. I also wouldn't bother with the "/resources" mapping since one is already provided by Spring Boot, albeit a different one than you defined (normally people put static resources in "classpath:/static" but "classpath:/resources" works as well and there is no prefix for the path to the resource in the HTTP endpoints).

JSP is inferior to other view technologies in so many ways, so it is unfortunate that it is so ubiquitous. There are many limitations, including restrictions on the way you can package and run a Boot application (see here for details). It would be worth your effort to unlearn JSP if you can spare the time.

Upvotes: 1

Related Questions