【问题标题】:Mapping some URLS with Spring Boot使用 Spring Boot 映射一些 URL
【发布时间】: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


    【解决方案1】:

    您应该尝试访问localhost:8080/Informacion/Listar,因为您已经映射到控制器类 (/Informacion)

    将jsp放入spring boot项目的首选位置是

    --webapp
           --WEB-INF
             --jsp
               --your.jsp
    

    在你的情况下 -

    -webapp
           --WEB-INF(Create WEB-INF directory if you don't have)
                --Vistas(folder)
             --jsp
               --hello.jsp
    

    您在属性文件中的视图路径将是spring.mvc.view.prefix=/WEB-INF/Vistas

    【讨论】:

    • 我的错误对不起,我实际上尝试了相同的 url localhost:8080/Informacion/Listar 但仍然得到相同的字符串但没有得到页面:/
    • 更改此spring.mvc.view.prefix=/webapp/Vistas/ 在末尾添加/@BryanSaltos
    • 我在末尾添加 / 现在我收到此错误:出现意外错误(类型 = 内部服务器错误,状态 = 500)。无法在名称为“dispatcherServlet”的 servlet 和控制台中解析名称为“ListarInformacion”的视图:javax.servlet.ServletException:无法在名称为“dispatcherServlet”的 servlet 中解析名称为“ListarInformacion”的视图
    • 您的 WEB-INF 文件夹在哪里??
    • 抱歉,迟到的答案有一些问题,顺便说一句,这是我所有的文件
    猜你喜欢
    • 1970-01-01
    • 2016-03-21
    • 2020-11-17
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 2017-11-13
    相关资源
    最近更新 更多