JSTL <c:redirect>
用于将当前页面重定向到另一个 URL。
<c:redirect>
语法:
<c:redirect url="http://www.anydomainhere.com/samplepage.jsp"/>
这是<c:redirect>
的方式。标签看起来像。我们只需要在此标签的 URL 属性中提供相对地址,页面将自动重定向加载时提供的 URL。
<c:redirect>
示例
在这里,我们根据变量myurl
的值将页面重定向到不同的 URL。如果值为 1,则页面将重定向到http://beginnersbook.com
,对于 2,它将转到http://www.google.com
。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> JSTL c:redirect Tag Example</title>
</head>
<body>
<c:set var="myurl" value="2" scope="request"/>
<c:if test="{myurl<1}">
<c:redirect url="http://beginnersbook.com"/>
</c:if>
<c:if test="{myurl>1}">
<c:redirect url="http://www.google.com"/>
</c:if>
</body>
</html>
输出:由于变量myurl
的值为 2,因此页面将定向到http://www.google.com
。