我试图在我的网页项目中建立一个链接,在链接文本中显示链接的网址。
例如,如果我正在处理本地主机的示例项目,我希望链接到example.jsp页面,使其看起来像http://localhost:8081/Example/example.jsp
它将链接到/example.jsp页面。
我需要能够动态地做到这一点。
发布于 2011-12-28 16:29:42
您可以使用JSTL获得站点的基本URL,如下所示:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="req" value="${pageContext.request}" />
<c:set var="url">${req.requestURL}</c:set>
<c:set var="base" value="${fn:substring(url, 0, fn:length(url) - fn:length(req.requestURI))}${req.contextPath}/" />
...(在上面的示例中,req只是当前实例的简写,<c:set var="url">行基本上将HttpServletRequest#getRequestURL()返回的StringBuffer转换为String,以便可以在字符串函数中使用)
然后,您可以创建如下链接:
<a href="${base}example.jsp">${base}example.jsp</a>或者,当使用HTML标记(使文档中的所有相对链接相对于<base> )时:
<head>
<base href="${base}" />
</head>
<body>
<a href="example.jsp">${base}example.jsp</a>
</body>https://stackoverflow.com/questions/8658457
复制相似问题