<c:url>标记和contextPath无法正确显示路径

时间:2016-02-06 16:19:36

标签: java spring jsp curl contextpath

我希望在点击特定文本时将客户端重定向到以下路径,但只有<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”该代码出了什么问题?

users.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"%>
<!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>

1 个答案:

答案 0 :(得分:0)

尝试替换它:

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

使用双引号""并添加var =“userVar”:

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

并使用您想要的${userVar}