java - 显示点击的特定链接的数据

标签 java spring jsp spring-mvc

我正在创建一个 spring mvc estore Web 应用程序。此处名为“showproducts.jsp”的 jsp 页面在表中呈现数据库中的所有产品。此处有一列名为“附加信息”。

<table class="table table-striped table-hover">
    <thead>
        <tr class="bg-success">
            <th>Product Name</th>
            <th>Info</th>
        </tr>
    </thead>

    <c:forEach var="product" items="${products}">
        <tr>
            <td>${product.productname}</td>
            <td><a href="${pageContext.request.contextPath}/pdtdetails">Additional info</a></td>
        </tr>
    </c:forEach>
</table>

我希望特定产品的 href 转到“pdtdetails”页面,并且单击时仅显示特定产品的所有详细信息。我在“pdtdetails.txt”中使用以下代码。 jsp”,但正如预期的那样,它显示所有产品的详细信息及其各自的数据,而不是唯一点击的产品。

<c:forEach var="product" items="${products}">
    <tr>
        productname:${product.productname}<br> 
        manufacturer:${product.manufacturer}<br>
        category:${product.category}<br>
        description:${product.description}<br> 
        units:${product.units}<br>
        price:${product.price}<br>
    </tr>
</c:forEach>

我应该如何表现出这种行为?

感谢您的宝贵时间。

最佳答案

你可以这样做:

<table class="table table-striped table-hover">
    <thead>
        <tr class="bg-success">
            <th>Product Name</th>
            <th>Info</th>
        </tr>
    </thead>
    <tbody>
    <c:forEach var="product" items="${products}">
        <tr>
            <td>${product.productname}</td>
            <td><a href="${pageContext.request.contextPath}/pdtdetails?id=${product.id}">Additional info</a></td>
        </tr>
    </c:forEach>
    </tobdy>
</table>

然后在详细信息页面中,从查询参数中获取所选项目并仅显示它:

<c:out value="${param['id']}"></c:out>

当然,您需要实现一个方法来从数据库中获取实体。

希望对你有帮助!

关于java - 显示点击的特定链接的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45092078/

相关文章:

java - 束中 Unresolved 约束,缺少要求 osgi.wiring.package

java - 通过对 apply 进行相应的函数调用来反射(reflect)一组更改

spring - 现有的 Spring 应用程序到 Cloud Foundry

java - 在Maven中设置环境变量

java - 如何确保池中的任务在程序退出前完成?

Javaagent 路径在 maven/linux/tomcat 上没有得到很好的解释

java - 休息 Controller 未在 Spring Boot 中映射

java - Spring MVC 处理列表中的按钮事件

java - 在jsp中捕获SQL Raiserror

javascript - Bootstrap 表未正确显示