我试图在Spring Boot中运行JSP,但显示的只是单词welcome
,而不是我的实际内容。调用MongoDB之后,将调用JSP。当我从Java代码调用MongoDB时,也会遇到这些异常。
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
at com.mongodb.internal.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:553) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:444) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:106) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:63) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongo-java-driver-3.9.1.jar:na]
at java.base/java.lang.Thread.run(Thread.java:844) [na:na]
Caused by: java.net.SocketTimeoutException: Read timed out
at java.base/java.net.SocketInputStream.socketRead0(Native Method) ~[na:na]
at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[na:na]
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:na]
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:na]
at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:109) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:570) ~[mongo-java-driver-3.9.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:441) ~[mongo-java-driver-3.9.1.jar:na]
... 9 common frames omitted
welcome.jsp
<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en" ng-app="employeeApp">
<head>
<title>Frank's App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="app.js"></script>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<h2>Frank's App</h2>
<div ng-app="myApp" ng-controller="myCtrl">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Employee Number</th>
<th>Pay Rate</th>
</tr>
<tr ng-repeat="employee in employees">
<td>{{ employee.firstName }}</td>
<td>{{ employee.lastName }}</td>
<td>{{ employee.empNumber }}</td>
<td>{{ employee.payRate }}</td>
</tr>
</table>
</div>
</body>
控制器代码:
@RequestMapping("/welcome")
public String welcome(Model model) {
model.addAttribute("employees", getEmployees());
return "welcome";
}
@RequestMapping("/getEmployees")
public List<Employee> getEmployees() {
MongoDatabase db = getDatabase();
MongoCollection<Document> collection = db.getCollection(COLLECTION);
Document query = new Document();
List<Employee> employees = collection.find(query, Employee.class).into(new ArrayList<Employee>());
return employees;
}
private MongoDatabase getDatabase() {
CodecRegistry pojoCodecRegistry = org.bson.codecs.configuration.CodecRegistries.fromRegistries(
MongoClientSettings.getDefaultCodecRegistry(), org.bson.codecs.configuration.CodecRegistries.fromProviders(PojoCodecProvider.builder().automatic(true).build()));
MongoDatabase database = mongo.getDatabase("testdb").withCodecRegistry(pojoCodecRegistry);
return database;
}
答案 0 :(得分:1)
您没有在HTML代码中使用JSP,而是Angular框架。 Angular不会读取您在控制器中作为“员工”属性推送的数据。
相反
<tr ng-repeat="employee in employees">
<td>{{ employee.firstName }}</td>
<td>{{ employee.lastName }}</td>
<td>{{ employee.empNumber }}</td>
<td>{{ employee.payRate }}</td>
</tr>
像这样使用JSP语法
<c:forEach var="employee" items="${employees}">
<tr>
<td>${employee.firstName}</td>
<td>${employee.lastName}</td>
<td>${employee.empNumber}</td>
<td>${employee.payRate}</td>
</tr>
</c:forEach>
如果您只想使用JSP构建网页,则也可以删除所有Angular元素(后缀为ng
的元素)。
答案 1 :(得分:0)
验证以下内容
检查您的mongodb服务器是否正在运行。服务器未运行可能会导致上述异常。
检查application.properties文件中的monogdb dbname,主机和端口
spring.data.mongodb.host =本地主机
spring.data.mongodb.port = *****
spring.data.mongodb.database = dbName
也检查这两个条目
spring.mvc.view.prefix = / WEB-INF / jsp /
spring.mvc.view.suffix = .jsp