引导Spring-boot应用程序时出现错误

时间:2019-06-07 18:39:02

标签: spring spring-boot

好吧,我正在通过选择View Technology作为JSP开发Spring Boot应用程序,但是当尝试自举Spring Boot应用程序时,我得到了白色错误页面。

模型类

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
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文件

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>

错误页面

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
  

好吧,请查看下面的代码。帮助我解决我所在的地方   弄错了吗?

2 个答案:

答案 0 :(得分:0)

您是否要启用自己的错误页面以禁用白电平错误页面?也许这可以帮助您。

如果您未在配置中指定任何自定义实现, 在Spring Boot中会自动注册BasicErrorController bean。您可以添加ErrorController的实现。

@Controller
public class MyErrorController implements ErrorController  {

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

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

答案 1 :(得分:0)

1)我建议尝试使用@RestController批注以确保您至少获得JSON响应。 (仅用于调试)

2)弄清楚第一部分后,您可以返回@Controller批注,并确保在 request mapping 方法中返回的 string 是可作为jsp文件使用。我建议最初尝试使用单个端点(“ /”),并为其使用适当的jsp页面。

3)如果仍然产生相同的问题,您可以参考这篇文章

Spring Boot JSP 404.Whitelabel Error Page

4)您也可以通过点击此链接https://www.baeldung.com/spring-boot-custom-error-page

来禁用和自定义默认错误页面。