1. 程式人生 > 實用技巧 >html-base標籤

html-base標籤

問題引入

頁面二使用a標籤請求轉發方式跳轉頁面一,

頁面一再使用a標籤相對路徑的跳轉方式跳轉到頁面二就會出錯。

解決方法:使用base標籤

<!-- base標籤設定頁面相對路徑工作時參照的地址
   herf屬性就是引數的地址值
   -->
<base href="http://localhost:8088/HtmlBase_war_exploded/a/b/c.html">

相關程式碼

package com.orz;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author orz * @create 2020-09-10 23:00 */ public class ForwardC extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException { req.getRequestDispatcher("/a/b/c.html").forward(req,resp); } }
View Code
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>這是首頁</title>

</head>
<body>
<p>這是web下的index首頁</p>
<a href="a/b/c.html">a/b/c.html</a><br/>
<a href="http://localhost:8088/HtmlBase_war_exploded/forwardC">請求轉發到a/b/c.html</a>
</body>
</html>
View Code
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- base標籤設定頁面相對路徑工作時參照的地址
       herf屬性就是引數的地址值
       -->
    <base href="http://localhost:8088/HtmlBase_war_exploded/a/b/c.html">
</head>
<body>
<p>這是a下的b下的c</p>
<a href="../../index.html">返回首頁</a>
</body>
</html>
View Code