2016-02-06 39 views
0

我想在單擊文本文本時將客戶端重定向到以下路徑,但只有<a href="${userUrl}"> Query </a><a href="${removeUserUrl}"> Remove </a>可正常工作。我的contextPath是「基礎」,jsp顯示路徑爲「http://localhost:8080/basics/」。當我點擊更新或添加用戶時,它會將我重定向到「http://localhost:8080/basics/basics/users」(jsp文件爲users.jsp),但它應該分別重定向到「http://localhost:8080/basics/user/update/ $ {customer.id}」和「http://localhost:8080/basics/user/add」該代碼有什麼問題?<c:url>標記和contextPath無法正確顯示路徑

users.jsp:對於使用雙引號""並加入VAR = 「USERVAR」

<a href="<c:url value='/user/add' />"> Add user </a> 

<%@ 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"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>Users</title> 

//...style 
</head> 
<body> 

    <c:set value="${pageContext.request.contextPath}" 
     var="contextUrl" /> 

    <c:set value="${contextUrl}/user/add" var="addUserUrl" /> 

    <table style="width: 100%"> 
     <tr> 
      <th>#ID</th> 
      <th>Name</th> 
      <th>Age</th> 
      <th>Country</th> 
      <td colspan="3" 
       style="background-color: #0CF323; text-align: center; border-top-color: #0CF323; border-right-color: #0CF323"> 
       <a href="<c:url value='/user/add' />"> Add user </a> 
      </td> 
     </tr> 

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

      <c:set value="${contextUrl}/user/update/${customer.id}" 
       var="updateUserUrl" /> 
      <c:set value="${contextUrl}/user/remove/${customer.id}" 
       var="removeUserUrl" /> 
      <c:set value="${contextUrl}/user/${customer.id}" var="userUrl" /> 

      <tr> 
       <td><c:out value="${customer.id }" /></td> 
       <td><c:out value="${customer.name }" /></td> 
       <td><c:out value="${customer.age }" /></td> 
       <td><c:out value="${customer.country.country }" /></td> 
       <td style="background-color: #17D0F5"><a href="${userUrl}"> Query </a></td> 
       <td style="background-color: #FF8000"><a 
        href="${updateUserUrl}"> Update </a></td> 
       <td style="background-color: #EC2727"><a 
        href="${removeUserUrl}"> Remove </a></td> 
      </tr> 
     </c:forEach> 
    </table> 


</body> 
</html> 

回答

0

嘗試更換此

<a href="<c:url value="/user/add" var="userVar" />">Add user</a> 

,並使用${userVar}你想要的地方。