java web jsp界面解决资源文件路径问题

    通常在界面中的资源文件比如js,图片,css等通过绝对路径和相对路径访问时,在你的机器或者在你部署的生产环境当时程序是没问题的但是当你换服务器很可能出问题,导致找不到文件,所以一般使用java直接得到服务路径代码如下,直接增加到jsp界面的头部

<%
    String path = request.getContextPath();
    String basepath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    pageContext.setAttribute("basepath",basepath);
%>

使用方式

<link rel="stylesheet" type="text/css" href="./css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="${basepath}css/bootstrap-theme.css"/>
<link rel="stylesheet" type="text/css" href="${basepath}widget/zTree/css/zTreeStyle/zTreeStyle.css"/>

效果

    <link rel="stylesheet" type="text/css" href="./css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="http://localhost:8080/lab/css/bootstrap-theme.css"/>
    <link rel="stylesheet" type="text/css" href="http://localhost:8080/lab/widget/zTree/css/zTreeStyle/zTreeStyle.css"/>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值