【发布时间】: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