2016-11-13 47 views
-1

在我的Java spring mvc web應用程序中,我想在鏈接中傳遞請求參數。下面是一個的.jsp頁代碼`<c:param> value屬性無法正常工作

  <c:url var = "updateLink" value="/customer/showFormForUpdate"> 
      <c:param name="customerId" value="${tempCustomer.id}"></c:param> 
      </c:url> 

      <c:forEach var="tempCustomer" items="${customers}"> 

      <tr> 
       <td> ${tempCustomer.firstName} </td> 
       <td> ${tempCustomer.lastName} </td> 
       <td> ${tempCustomer.email} </td> 
       <td> ${tempCustomer.id} </td> 



       <td> <a href="${updateLink}">Update</a> </td> 

下面的代碼的部分工作完全fine..It顯示客戶的ID爲「tempCustomer」的部分。

<td> ${tempCustomer.id} </td> 

然而,

<c:url var = "updateLink" value="/customer/showFormForUpdate"> 
     <c:param name="customerId" value="${tempCustomer.id}"></c:param> 

的代碼不工作精細這一部分。值=「$ {tempCustomer.id}」只是不傳遞tempCustomer.id的值。當我點擊,更新鏈接

<td> <a href="${updateLink}">Update</a> </td> 

的URL看起來像這樣

http://localhost:8080/web-customer-tracker/customer/showFormForUpdate?customerId=

參數值是不存在的。 url應該包含customerId參數的值。

對於完整的參考,這裏是全的.jsp頁面代碼

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<!DOCTYPE html> 
<html> 
<head> 
<title>List Customers</title> 




<link type="text/css" rel="stylesheet" 
href="${pageContext.request.contextPath}/resources/css/style.css"/> 


</head> 
<body> 

     <div id ="wrapper"> 
      <div id ="header"> 
       <h2>CRM - Customer Relationship Manager</h2> 
      </div> 
     </div> 

     <div id ="container"> 

      <div id = "content"> 

      <input type="button" value="Add Customer" 
      onclick="window.location.href='showFormForAdd';return false;" 
      class="add-button"   /> 


      <table> 
       <tr> 
        <th>First Name</th> 
        <th>Last Name</th> 
        <th>Email</th> 
       </tr> 

       <!-- ${tempCustomer.id} 
       --> 
       <c:url var = "updateLink" value="/customer/showFormForUpdate"> 
       <c:param name="customerId" value="${tempCustomer.id}"></c:param> 
       </c:url> 

       <c:forEach var="tempCustomer" items="${customers}"> 

       <tr> 
        <td> ${tempCustomer.firstName} </td> 
        <td> ${tempCustomer.lastName} </td> 
        <td> ${tempCustomer.email} </td> 
        <td> ${tempCustomer.id} </td> 



        <td> <a href="${updateLink}">Update</a> </td> 


       </c:forEach> 

      </table> 


      </div> 

     </div> 

</body> 
</html> 

回答

0
 <c:url var = "updateLink" value="/customer/showFormForUpdate"> 
      <c:param name="customerId" value="${tempCustomer.id}"></c:param> 
     </c:url> 

     <c:forEach var="tempCustomer" items="${customers}"> 

     <tr> 
      <td> ${tempCustomer.firstName} </td> 
      <td> ${tempCustomer.lastName} </td> 
      <td> ${tempCustomer.email} </td> 
      <td> ${tempCustomer.id} </td> 

你在你的C時定義的tempCustomer變量:的forEach。它只存在於c:forEach的範圍中。移動你的c:url塊下的c:forEach。

+0

謝謝大聲笑。這是一個小白菜錯誤! –

+0

發生在我們身上;-) –