0

Well i am developing a spring boot application by choosing view technology as jsp.But when am trying to bootstraping the spring-boot application i am getting white level error page.

Model Class

public class Person {

    private String p_first_name;
    private String p_last_name;
    private int age;
    private String city;
    private String state;
    private String country;

    public Person(String p_first_name, String p_last_name, int age, String city, String state, String country) {
        super();
        this.p_first_name = p_first_name;
        this.p_last_name = p_last_name;
        this.age = age;
        this.city = city;
        this.state = state;
        this.country = country;
    }

    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }

    public String getP_first_name() {
        return p_first_name;
    }

    public void setP_first_name(String p_first_name) {
        this.p_first_name = p_first_name;
    }

    public String getP_last_name() {
        return p_last_name;
    }

    public void setP_last_name(String p_last_name) {
        this.p_last_name = p_last_name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

}

Controller Class

@Controller
public class PersonController {

    private static ArrayList<Person> persons = new ArrayList<Person>();

    static {
        persons.add(new Person("kumar", "bikash", 28, "bangalore", "karnataka", "india"));
        persons.add(new Person("kumar", "pratap", 24, "delhi", "delhi", "india"));
        persons.add(new Person("kumar", "ravi", 29, "delhi", "delhi", "india"));
        persons.add(new Person("kumar", "mangalam", 65, "delhi", "delhi", "india"));
    }

    @RequestMapping(value = { "/", "/index" }, method = RequestMethod.GET)
    public String index(Model model) {
        String message = "Hello" + "Spring Boot implementation with jsp Page";
        model.addAttribute("message", message);
        return "index";
    }

    @RequestMapping(value = "/personList", method = RequestMethod.GET)
    public String getPersonList(Model model) {
        model.addAttribute("persons", persons);
        return "personList";

    }
}

application.properties

# VIEW RESOLVER CONFIGURATION
spring.mvc.view.prefix=/WEB-INF/jsp
spring.mvc.view.suffix=.jsp

jsp file

index.jsp
=========
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Integration of Spring Boot with jsp page</title>
</head>
<body>
    <h1>Welcome to Spring boot</h1>
    <p>This project is an Example of how to integrate Spring Boot with
        jsp page.</p>
        <h2>${message} </h2>

        <a href="${pageContext.request.ContextPath}/personList"></a>
</body>
</html>

personList.jsp
==============
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Person List content Present here</title>
</head>
<body>
    <h1>Person List</h1>

    <div>
        <table border="1">
            <tr>
                <th>FirstName:</th>
                <th>LasttName:</th>
                <th>Age:</th>
                <th>city:</th>
                <th>State:</th>
                <th>Country:</th>
            </tr>
            <c:forEach items="${persons}" var=person>
                <tr>
                    <td>${person.firstname}</td>
                    <td>${person.lastname}</td>
                    <td>${person.age }</td>
                    <td>${person.city }</td>
                    <td>${person.state }</td>
                    <td>${person.country }</td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>

Error page

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jun 07 23:41:57 IST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

well please review the below code.Help me to resolve thing where i am getting wrong?

  • Would you please show me your project tree, i just want to see the location of your views. Note that If your're using STS tools in Eclipse or Spring intializer in IntelliJ to build your project, in case of not specifying any configuration for the spring mvc in your "application.properties" file, the default location of your views is the resources/templates folder. – El-MAHMOUDI Jun 07 '19 at 19:20
  • well i have created spring boot project by using spring initializer and import the project into sts.For displaying dynamic content i have created 3 different directory inside src/main.The directory i have created in the order as webapp,WEB-INF,jsp.In the jsp directory i have written two jsp pages. – Bikash Mohapatra Jun 07 '19 at 19:54
  • okey, under the src/main create a folder and name it "resources". Put your application.properties file in the newly created folder. Then create "src/main/templates" folder where you will place your views. After moving your views to the src/main/templates folder, comment the spring mvc configuration you made in the application.properties. Run your Spring boot application to see the result. Follow what i've recommanded and keep me updated about the result. – El-MAHMOUDI Jun 07 '19 at 20:25
  • Well src/main/template folder is used for representing only for static content and for defining error pages. – Bikash Mohapatra Jun 07 '19 at 20:44
  • Well as per your suggestion i have did my changes but am getting the same http status 404.I have move all my jsp page to src/main/resources/template directory.Still i am facing the issue. – Bikash Mohapatra Jun 07 '19 at 20:44
  • be careful about the spellings, its "templates" not template, so it's "src/main/resources/templates". And don't forget to remove these two lines: "spring.mvc.view.prefix=/WEB-INF/jsp spring.mvc.view.suffix=.jsp" from your application.properties file. Let's make the application work first, then we will discuss about these 2 lines, but for instance comment or remove them. – El-MAHMOUDI Jun 07 '19 at 23:46

2 Answers2

0

Are you looking to enable your own errorpage disabling the white level error page? May be this can help you.

If you do not specify any custom implementation in the configuration, BasicErrorController bean is automatically registered in Spring Boot. You can add your implementation of ErrorController.

@Controller
public class MyErrorController implements ErrorController  {

    @RequestMapping("/error")
    public String handleError() {
        //do something like logging
        return "error";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}
Jaswinder Singh
  • 731
  • 5
  • 20
  • Can you share the server startup log. Also verify if you have added component scan annotation to Application class. @ComponentScan(basePackages={"com.package"}) – Jaswinder Singh Jun 07 '19 at 19:10
  • Well i have not added ComponentScan annotation because i am using SpringbootApplication Annotation to bootstrap my application.Internally @ComponentScan annotation will work and scan all the packages and configure all this class as a bean inside Application Context. – Bikash Mohapatra Jun 07 '19 at 20:01
  • For ErrorController class do we have to use @ComponentScan anotation or not? Please clarify me here? – Bikash Mohapatra Jun 07 '19 at 20:03
  • Well thank you for helping to how to server your own custom error page in spring boot.As per suggestion i go and my issues got resolved from not showing white level Error page to Custom Error page.But i am still facing the issues to bootstrap the application ans showing real content what i intended for. – Bikash Mohapatra Jun 07 '19 at 20:48
0

1) I would suggest trying the @RestController annotation to make sure that you get at least the JSON response. (Only for Debugging)

2) After the first part is figured out, you can go back to your @Controller annotation and make sure that the string you return in the request mapping method is available as a jsp file. I would recommend trying with a single endpoint initially ("/") and having the appropriate jsp page for it.

3) If it still produces the same issue, you can refer to this post

Spring Boot JSP 404.Whitelabel Error Page

4) You can also disable and customize the default error page by following this link https://www.baeldung.com/spring-boot-custom-error-page

RLM
  • 54
  • 5