为什么<a href="#">点击后的链接不是当前页面,而是项目根目录后加#
问题:我在http://localhost/:8080/ssm0702/user/getUsers写了个登录的超链接href="#",我以为他会跳转到当前页面,结果跳转到http://localhost/:8080/ssm0702/#了
截图:
页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'allUsers.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<c:if test="${uname==null }"><a href="#">登录</a></c:if>
<c:if test="${uname!=null }">欢迎,${uname }登录!</c:if>
<c:if test="${uname!=null }"><a href="<%=basePath %>user/loginout">注销</a></c:if>
<center>
<h1>用户列表</h1>
<table border="1">
<tr>
<td>用户编号</td>
<td>用户名</td>
<td>密码</td>
</tr>
<c:forEach items="${users }" var="d">
<tr>
<td><a href="#">${d.userid }</a></td>
<td>${d.username }</td>
<td><img alt="${d.pwd}" src="img/${d.pwd}"></td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
回答:
你为你页面设置了 base.
<base href="<%=basePath%>">
不过不管有没有 base, 页面最后都是会被加上#的, 这是标准定义的行为. 基于你的需求, 你可以这么做
<a href="javascript:void(0)">登录</a>
回答:
<base href="<%=basePath%>">
以上是 为什么<a href="#">点击后的链接不是当前页面,而是项目根目录后加# 的全部内容, 来源链接: utcz.com/p/178697.html