【问题标题】:Getting Error while bootstraping the Spring-boot Application引导 Spring-boot 应用程序时出错
【发布时间】:2019-06-08 13:52:01
【问题描述】:

我正在通过选择视图技术作为 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

请查看以下代码。帮助我解决我所在的问题 搞错了吗?

【问题讨论】:

  • 能否请您给我看一下您的项目树,我只是想看看您的视图的位置。请注意,如果您使用 Eclipse 中的 STS 工具或 IntelliJ 中的 Spring intializer 来构建您的项目,如果没有在“application.properties”文件中为 spring mvc 指定任何配置,则视图的默认位置是资源/templates 文件夹。
  • 好吧,我已经使用 spring 初始化程序创建了 spring boot 项目并将项目导入到 sts。为了显示动态内容,我在 src/main 中创建了 3 个不同的目录。我创建的目录顺序为webapp,WEB-INF,jsp。在jsp目录下我写了两个jsp页面。
  • 好的,在 src/main 下创建一个文件夹并将其命名为“resources”。将您的 application.properties 文件放在新创建的文件夹中。然后创建“src/main/templates”文件夹,您将在其中放置视图。将视图移动到 src/main/templates 文件夹后,注释您在 application.properties 中所做的 spring mvc 配置。运行您的 Spring 引导应用程序以查看结果。遵循我的建议,让我了解结果。
  • Well src/main/template 文件夹仅用于表示静态内容和定义错误页面。
  • 根据您的建议,我已经进行了更改,但得到了相同的 http 状态 404。我已将所有 jsp 页面移动到 src/main/resources/template 目录。我仍然面临这个问题.

标签: spring spring-boot


【解决方案1】:

您是否希望启用您自己的错误页面来禁用白级错误页面?可能这可以帮助你。

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

@Controller
public class MyErrorController implements ErrorController  {

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

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

【讨论】:

  • 能否分享一下服务器启动日志。还要验证您是否已将组件扫描注释添加到应用程序类。 @ComponentScan(basePackages={"com.package"})
  • 好吧,我没有添加 ComponentScan 注释,因为我正在使用 SpringbootApplication 注释来引导我的应用程序。在内部,@ComponentScan 注释将工作并扫描所有包并将所有此类配置为应用程序上下文中的 bean。
  • 对于ErrorController类,我们是否必须使用@ComponentScan注解?请在这里澄清一下?
  • 嗯,感谢您帮助如何在 Spring Boot 中提供您自己的自定义错误页面。根据建议,我去了,我的问题从不显示白级错误页面到自定义错误页面得到了解决。但是我我仍然面临引导应用程序和显示我想要的真实内容的问题。
【解决方案2】:

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来禁用和自定义默认错误页面

【讨论】:

    猜你喜欢
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 2017-09-04
    相关资源
    最近更新 更多