[英]How to pass data from MongoDb to Jsp Spring boot
I am beginner to spring mvc. 我是Spring MVC的初学者。 And I'm trying to pass data from mongodb to JSP.
我正在尝试将数据从mongodb传递到JSP。 I've seen tutorials and answers in other sites but I donno whay am I missing.
我在其他网站上也看到过教程和答案,但我不知道我在想什么。
The error I get : 我得到的错误:
java.lang.NumberFormatException: For input string: "url" at java.lang.NumberFormatException.forInputString(Unknown Source) ~[na:1.8.0_144] at java.lang.Integer.parseInt(Unknown Source) ~[na:1.8.0_144].....
java.lang.NumberFormatException:对于输入字符串:java.lang.NumberFormatException.forInputString(未知来源)处的“ URL”〜java.lang.Integer.parseInt(未知来源)处的[na:1.8.0_144]〜[na:1.8 .0_144] .....
Here's my code : 这是我的代码:
Service: 服务:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.javabrains.springbootstarter.dao.TopicRepository;
import io.javabrains.springbootstarter.model.Topic;
@Service
public class TopicServic {
@Autowired
private TopicRepository topicRepository;
public List<Topic> getAllTopics(){
return (List<Topic>) topicRepository.findAll();
}
public Topic getTopic(String id) {
//return topics.stream().filter(t -> t.getId().equals(id)).findFirst().get();
return topicRepository.findOne(id);
}
public void addTopic(Topic topic) {
topicRepository.save(topic);
}
public void updateTopic(String id, Topic topic) {
topicRepository.save(topic);
}
public void deleteTopic(String id) {
topicRepository.delete(id);
}
}
Controller: 控制器:
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import io.javabrains.springbootstarter.model.Topic;
import io.javabrains.springbootstarter.service.TopicServic;
@Controller
public class TopicController {
@Autowired
private TopicServic topicServic;
public TopicController(TopicServic topicServic) {
this.topicServic = topicServic;
}
@GetMapping("/topics")
public String ListDocuments(Model model) {
model.addAttribute("topics", topicServic.getAllTopics());
return "/bonjour";
}
}
JSP "/bonjour": .... JSP“ / bonjour”: ..
.....
<table class="table table-striped table-bordered text-left">
<thead>
<tr>
<th>Url</th>
<th>Name</th>
<th>Description</th>
<th>Tags</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<c:forEach var="topics" items="${topics}">
<tr>
<td>${topic.url}</td>
<td>${topic.name}</td>
<td>${topic.description}</td>
<td>${topic.tags}</td>
<td>${topic.type}</td>
</tr>
</c:forEach>
</tbody>
</table>
your var
is spelt wrong, should have been topic
not topics
您的
var
拼写错误,应该是topic
而不是topics
<c:forEach var="topic" items="${topics}">
<tr>
<td>${topic.url}</td>
<td>${topic.name}</td>
<td>${topic.description}</td>
<td>${topic.tags}</td>
<td>${topic.type}</td>
</tr>
</c:forEach>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.