java jstl 的 c:url 标签的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41456615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:53:55  来源:igfitidea点击:

What is the purpose of jstl's c:url tag?

javajstl

提问by Ole

TutorialPointhas a simple example of the c:urltag that looks like this:

TutorialPoint有一个简单的c:url标签示例,如下所示:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:url> Tag Example</title>
</head>
<body>
<a href="<c:url value="/jsp/index.htm"/>">TEST</a>
</body>
</html>

When looking at the corresponding rendering with Chrome's developer tooling it renders like this:

当使用 Chrome 的开发人员工具查看相应的渲染时,它会呈现如下:

<a href="/jsp/index.htm">TEST</a>

So the c:urltag seems redundant, but I'm sure I'm missing something?

所以c:url标签似乎是多余的,但我确定我遗漏了什么?

回答by msagala25

As Tutorials Points says, It is for formattingpurposes of the URLyou put in and it can be stored in a variable.

作为教程点说,这是格式化的目的URL你把它可以存储在一个变量

Example you have this:

例如你有这个:

<a href="<c:url value="/test.html"/>">TEST</a>

if you click TEST, it will go to page test.html. simple as that. but the question is, what is the value of <c:url value="/test.html"/>?

如果您单击TEST,它将转到页面test.html。就那么简单。但问题是, 的价值是<c:url value="/test.html"/>什么?

are you thinking the value is only /test.html?

你以为只有价值/test.html吗?

try to test it, like this:

尝试测试它,像这样:

<a href="<c:url value="/test.html" var="testvar" />">TEST</a> // testvar is where you put the url formatted by c:url
<c:out value="${testvar}"/> // you print what is the formatted url

the answer will be the Context Folderof your project plus the URLyou put in.

答案将是您项目的上下文文件夹加上您输入的 URL

context/test.html will be the output.

上下文/test.html 将是输出。

I think that its purpose is to have the context(Current Application) already given to the URL, and you only need to add the remaining URLpart.

我认为它的目的是将context(当前应用程序)已经提供给URL,您只需要添加其余URL部分。

回答by LexSav

Here is a short snippet from my training app where I use <c:url>tag:

这是我使用<c:url>标签的训练应用程序中的一个简短片段:

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

    <c:forEach var="student" items="${student_list}">

        <c:url var="loadStudentLink" value="StudentControllerServlet">
            <c:param name="command" value="load"/>
            <c:param name="id" value="${student.id}"/>
        </c:url>

        <tr>
            <td> ${student.firstName} </td>
            <td> ${student.lastName} </td>
            <td> ${student.email} </td>
            <td> 
                <a href="${loadStudentLink}">Update</a>
            </td>
        </tr>   

    </c:forEach>                
</table>

Of course in this case I could just use the link below and it would be the same:

当然,在这种情况下,我可以只使用下面的链接,它会是一样的:

<a href="StudentControllerServlet?command=load&id=${student.id}">Update</a>

In a nutshell, <c:url>creates an ordinary link which you can store in a variable and define its scope. With <c:param>tags you can set parameters for the link. In addition, some could say that it looks more neatly with JSTL.

简而言之,<c:url>创建一个普通链接,您可以将其存储在变量中并定义其范围。使用<c:param>标签,您可以为链接设置参数。另外,有人会说用JSTL看起来更整洁。

Also, as it was said before, <c:url>already has context path of the app. So, for instance, you can do this

<link rel="stylesheet" href="<c:url value="/resources/css/test.css"/>" />

instead of that

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

此外,正如之前所说,<c:url>已经有了应用程序的上下文路径。因此,例如,您可以这样做

<link rel="stylesheet" href="<c:url value="/resources/css/test.css"/>" />

而不是那样

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

I hope you catch my meaning :)

我希望你明白我的意思:)

回答by LazerBass

In addition to the purposes already mentioned in the other answers an additional purpose of the c:urltag is to rewrite URLs returned from a JSP page. For example for session tacking. In case the user agent does not support (or has disabled) cookies the jsessionid will be appended to the URL.

除了其他答案中已经提到的目的之外,该c:url标记的另一个目的是重写从 JSP 页面返回的 URL。例如用于会话跟踪。如果用户代理不支持(或已禁用)cookie,则 jsessionid 将附加到 URL。

The tag includes the session ID in the URL only if cookies are disabled; otherwise, it returns the URL unchanged.

仅当 cookie 被禁用时,该标签才会在 URL 中包含会话 ID;否则,它将返回未更改的 URL。

See: https://docs.oracle.com/javaee/5/tutorial/doc/bnakh.html#bnakn

请参阅:https: //docs.oracle.com/javaee/5/tutorial/doc/bnakh.html#bnakn