Home > Back-end >  Not getting to the index.jsp page in spring boot
Not getting to the index.jsp page in spring boot

Time:09-17

When I go to http://localhost:8080/ for my spring boot form it just gives me a whitelabel error page. This is my Controller code


package net.codejava;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MvcController {

    @RequestMapping("/")
    public String home() {
        System.out.println("Going home...");
        return "index";
    }
}

and here's my index.jsp code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Volunteer Management Form</title>
</head>
<body>
    <h1>Volunteer Management Form</h1>
</body>
</html>

I cannot work out why it won't show I do however get "Going home..." printed in the console

CodePudding user response:

i think that you have problem with the view resolver add this to your application.properties:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

and move your index.jsp to /WEB-INF/jsp/

the second method is to set resolver by using WebConfig class like that : add this class to your source package :

@EnableWebMvc
@Configuration
@ComponentScan("net.codejava")
public class WebConfig implements WebMvcConfigurer {
    @Bean
    public ViewResolver internalResourceViewResolver() {
    InternalResourceViewResolver bean = new 
    InternalResourceViewResolver();
    bean.setViewClass(JstlView.class);
    bean.setPrefix("/WEB-INF/view/");
    bean.setSuffix(".jsp");
    return bean;
   }

}

CodePudding user response:

Not sure how did you setup JSP on spring boot because there's some specific dependencies that you need to have. Also, people nowadays use Thymeleaf or Freemarker for templating instead of JSP on spring boot. I was able to follow and run the github project from https://www.baeldung.com/spring-boot-jsp with these urls

  • http://localhost:8080/spring-boot-jsp/book/viewBooks
  • http://localhost:8080/spring-boot-jsp/book/addBook
  • Related