有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpring:mvcUrl映射错误的id

我试图将productId映射到jsp页面中的URL,但该值是错误的。 URL正在返回<a href="/casadocodigo/produtos/%7B/id%7D">Título 1</a>,但响应正在返回正确的值:

HTML Mapping

我的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"%>
    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <div>
            ${success}
        </div>
        <table>
            <tr>
                <td>Id</td>
                <td>Titulo</td>
                <td>Valores</td>
    </tr>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.id}</td>
            <td><a href="${spring:mvcUrl('PC#show').arg(0, product.id).build()}">${product.title}</a></td>
            <td>
                <c:forEach items="${product.prices}" var="price">
                    [${price.value} - ${price.bookType}]
                </c:forEach>
            </td>
        </tr>
    </c:forEach>
</table>

我的Java控制器代码:

    @RequestMapping(method=RequestMethod.GET)
public ModelAndView list() {
    ModelAndView modelAndView = new ModelAndView("products/list");
    modelAndView.addObject("products", productDAO.list());
    return modelAndView;
}

我的Java DAO代码:

    public List<Product> list() {
    return manager.createQuery("select distinct(p) from Product p join fetch p.prices", Product.class).getResultList();
}

共 (1) 个答案

  1. # 1 楼答案

    很遗憾,我看不到您的方法“show”。 我猜您的路径中有一个打印错误:

    @RequestMapping("/show{/id}")
    //          ^^  WRONG
    public String show(@PathVariable("id") long id) {
        ...
    }
    

    反而

    @RequestMapping("/show/{id}")
    //          ^^  RIGHT