引导 Spring 引导应用程序时出错



好吧,我正在通过选择视图技术作为jsp来开发一个spring启动应用程序。但是当我尝试引导 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";
    }
}

应用程序属性

# 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

那么请查看下面的代码。帮助我解决我所在的地方 弄错了?

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

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

@Controller
public class MyErrorController implements ErrorController  {
    @RequestMapping("/error")
    public String handleError() {
        //do something like logging
        return "error";
    }
    @Override
    public String getErrorPath() {
        return "/error";
    }
}

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

2(弄清楚第一部分后,你可以回到你的@Controller注解,并确保你在请求映射方法中返回的字符串可以作为jsp文件使用。我建议最初尝试使用单个端点("/"(,并为其提供适当的 jsp 页面。

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

Spring Boot JSP 404.Whitelabel Error Page

4( 您还可以通过以下链接禁用和自定义默认错误页面 https://www.baeldung.com/spring-boot-custom-error-page

相关内容

最新更新