表单form action的路径影响因素探究——base标签

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/superit401/article/details/82666180

以login.jsp为例,访问路径为localhost:8080/app/login.jsp,代码如下:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();

%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<!-- base标签在此处 -->
<base href="<%=basePath%> ">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- <meta http-equiv="prama" content="no-cache">
<meta http-equiv="cache-control" content="no-cache"> -->
<title>登录页面</title>
</head>
<body>
	<p style="color:red;">${errorMsg}</p><br/>
	<a href="showGirls">去看美女</a><br/>

	<!-- http://localhost:8080/app/login -->
<%-- 	${web.context.path}/login --%>

	<form action="login" method="post">
		用户名:<input type="text" name="userName" value="" /><br/>
		密码:<input type="password" name="password" value="" /><br/>
		<input type="submit" value="登录" />
	</form>
</body>
</html>

1.base标签会对表单提交后的跳转的url产生影响。

(1)本地起项目后,login.jsp中的basePath为http://localhost:8080

2.当form中的action不以斜杠/开头的则是相对当前你访问本页面的路径,即针对本页面路径的相对路径。当时以斜杠/开头的则是应用的根目录。

3. base路径的影响分情况讨论:

  • 没有配置base标签:

(1)form中的action="login",提交表单后浏览器地址栏中的url为http://localhost:8080/app/login,正常

(2)form中的action="/login",提交表单后浏览器地址栏中的url为http://localhost:8080/login,404

(3)form中的action="${web.context.path}/login",提交表单后浏览器地址栏中的url为http://localhost:8080/app/login,正常

(4)form中的action="/${web.context.path}/login",提交表单后浏览器地址栏中的url为http://app/login,无法访问此网站

  • 配置了base标签:

(1)form中的action="login",提交表单后浏览器地址栏中的url为http://localhost:8080/login,404

(2)form中的action="/login",提交表单后浏览器地址栏中的url为http://localhost:8080/login,404

(3)form中的action="${web.context.path}/login",提交表单后浏览器地址栏中的url为http://localhost:8080/app/login,正常

(4)form中的action="/${web.context.path}/login",提交表单后浏览器地址栏中的url为http://app/login,无法访问此网站

4. base对a标签的href的影响同form中的action

猜你喜欢

转载自blog.csdn.net/superit401/article/details/82666180