3

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.

user3386275
  • 65
  • 1
  • 2
  • 10
  • if I try to remove SetSuffix(".jsp"), localhost:8080/index also returns a 404 error which means that java is reading my code for viewresolver. But how come I cannot map index.jsp to index.jsp when I set suffix to ".jsp"? What configuration am I missing? – user3386275 Mar 06 '14 at 04:43
  • That is how you have configured things and is also how things should work. You shouldn't access your jsp files directly (nor will you be able to as they are under `/WEB-INF`. You will have to let Spring resolve the JSP (and hence the `/index` works because spring is handling that). In short works as intended (maybe as not as you intended but alas). – M. Deinum Mar 06 '14 at 06:40
  • Thanks M. Deinum. It all makes sense now. I missed that concept of things under /WEB-INF won't be directly accessible. – user3386275 Mar 06 '14 at 07:54
  • And I missed that best practice that jsp files shouldn't be directly accessible. – user3386275 Mar 06 '14 at 08:04

4 Answers4

1

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.

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
1

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

FriendlyMikhail
  • 2,857
  • 23
  • 39
1

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.

tyrantqiao
  • 319
  • 4
  • 7
-1

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.

RPaul
  • 946
  • 10
  • 11