【发布时间】:2018-12-14 05:29:14
【问题描述】:
嗨,我是新的 spring boot 和 mvc 模型我试图创建一些方法来调用一些数据到 Mongo D.B 使用邮递员我正在获取数据但现在我想在网页中显示这些数据但我无法弄清楚映射在 Spring Boot 中是如何工作的。 我有这个方法:
@Controller
@RequestMapping("/Informacion")
public class InformacionControlador {
@Autowired
private informacionRepo informacioRepo;
public InformacionControlador(informacionRepo informacioRepo) {
this.informacioRepo = informacioRepo;
}
//This method is comment because using postman get the answer in json format
// @GetMapping("/Listar")
// public List<Informacion> getAll() {
// List<Informacion> info = this.informacioRepo.findAll();
// return info;
// }
//Now this is the method that i want to work like the first one but
//instead of json answer y want to see the data in ListarInformacion page
@GetMapping("/Listar")
public String informacion(ModelMap model) {
model.addAttribute("info", informacioRepo.findAll());
return "ListarInformacion";
}
@PutMapping
public void insert(@RequestBody Informacion informacion) {
this.informacioRepo.insert(informacion);
}
}
我还把这行放在 application.properties 文件中以设置存储页面的文件夹
spring.mvc.view.prefix=/webapp/Vistas
spring.mvc.view.suffix=.jsp
这是我的 ListarInformacion 页面
<html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<head>
<title>Title</title>
</head>
<body>
<form:form method="GET" action="/webapp/Vistas/ListarInformacion" modelAttribute="Informacion">
<table class="table table-striped table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Nombre</th>
<th scope="col">Identificación</th>
<th scope="col">Fecha Creación</th>
</tr>
</thead>
<c:forEach var="p" items="${info}">
<tr>
<td>${p.idInformacion}</td>
<td>${p.nombre}</td>
<td>${p.pais}</td>
<td><fmt:formatDate pattern="dd/MM/yyyy" value="${p.fechaCreacion}"/></td>
<td>
</td>
</tr>
</c:forEach>
</table>
</form:form>
</body>
</html>
this is the location of my files
谁能告诉我我错过了什么,因为当我尝试访问 url localhost:8080/Informacion/Listar 时,答案是一个字符串,上面写着 /ListarInformacion ant 它不会将我重定向到它应该重定向的页面
【问题讨论】:
标签: mongodb spring-mvc spring-boot jsp jstl