zoukankan      html  css  js  c++  java
  • 在Jsp中调用静态资源,路径配置问题

    在Jsp中调用图片、JS脚本等,针对取得的路径有两种调用方式:

    1、放入Body中生成绝对路径(不建议)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>image调用</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    </head>
    <body>
        <h1>图片访问</h1>
        <div>        
            <img alt="图片" src="<%=basePath%>/image/a.png">
        </div>
    </body>
    </html>

    2、在Head中指定,Body中使用相对路径(建议)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>image调用</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
        <!-- base需要放到head中 -->    
        <base href=" <%=basePath%>">   
    </head>
    <body>
        <h1>图片访问</h1>
        <div>     
            <img alt="图片" src="image/a.png">
        </div>
    </body>
    </html>

  • 相关阅读:
    (22)C#windows打包部署
    (2)OLEDB数据库操作
    (5)C#工具箱-数据
    (21)C#VS快捷键
    (1)OracleClient数据库操作(淘汰)
    (4)C#工具箱-菜单和工具栏
    (3)C#工具箱-容器
    (2)C#工具箱-公共控件2
    (9)oracle 表的基本查询
    企鹅
  • 原文地址:https://www.cnblogs.com/maocs/p/4256302.html
Copyright © 2011-2022 走看看