jsp页面显示 项目外部资源(如:本地图片),server.xml配置 虚拟路径

 

目录

解决方案1、server.xml文件中,配置虚拟路径

步骤1、imgsrc_virtualUrl.jsp

步骤2、server.xml中配置虚拟路径

解决方案2、封装配置工具,相当于server.xml文件中,配置虚拟路径


 

 

解决方案1、server.xml文件中,配置虚拟路径

步骤1、imgsrc_virtualUrl.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	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>页面访问外部资源(如:本地图片)——虚拟路径演示</title>
<!--
http://localhost:8081/SpringMVC_JDBC/view/general_jsp/simpleJsp/imgsrc_virtualUrl.jsp
-->
<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>
	<span>访问外部资源——使用图片虚拟路径写法1、 </span>
	<img src="http://localhost:8081/myvirtual/file/oneCutPic.jpg" alt="图片预览2">
	<br> 
	<span>访问外部资源——使用图片虚拟路径写法2、</span>
	 <img src="/myvirtual/file/oneCutPic.jpg" alt="图片预览2">
</body>
</html>
<!-- 
	一、必须在,下载的tomcat server.xml中
	<Host> 
		//在此处配置虚拟路径,解决谷歌浏览器禁止直接访问磁盘文件问题。其中,增加的path="/虚拟名" docBase="虚拟路径"。jsp页面的src虚拟路径下,文件名称"不要用中文"
		<Context path="/myvirtual/file" docBase="D:\ljx\test\" reloadable="true"></Context>
	</Host>
	
	二、**.jsp页面	
	写法1、
	<img src='http://ip+端口/虚拟路径/文件夹/文件名'>
	<img src="http://localhost:8081/myvirtual/file/oneCutPic.jpg" alt="图片预览2" > 
	写法2、
	<img src='/虚拟路径/文件夹/文件名'>
	<img src="/myvirtual/file/oneCutPic.jpg" alt="图片预览2" > 
-->

步骤2、server.xml中配置虚拟路径

1、双击tomcat7服务器》切换 Overview窗口》如果Server Locations使用的是,eclipse“默认server.xml”。
这里要注意一下:如果编程使用的是eclipse的话,他的tomcat插件里每次重启动是会更新server.xml的,所以需要改eclipse默认的server.xml配置。其位置一般在workspace下的Servers文件夹中,例如
D:\DevelopSoft\JavaWeb\Eclipse\eclipseWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\conf\server.xml。
(方法1)可在默认server.xml中配置虚拟路径。
<Host> 
    //在此处配置虚拟路径,解决谷歌浏览器禁止直接访问磁盘文件问题。其中,增加的path="/虚拟名" docBase="虚拟路径"。jsp页面的src虚拟路径下,文件名称"不要用中文"
    <Context path="/myvirtual/file" docBase="D:\ljx\test\" reloadable="true"></Context>
</Host>

(方法2)双击tomcat7服务器》切换 Modules窗口》点击 Add External Web Module...》3、Document base:点击Browse...(选择项目外资源路径)>Path:/myvirtual/file》点击确定

 

  2、双击tomcat7服务器》切换 Overview窗口》如果Server Locations使用的是,下载的tomcat7下的“server.xml”。
例如D:\DevelopSoft\JavaWeb\Tomcat\tomcat9_8088\conf\servre.xml。
(方法1)可在server.xml中配置虚拟路径。
<Host> 
    //在此处配置虚拟路径,解决谷歌浏览器禁止直接访问磁盘文件问题。其中,增加的path="/虚拟名" docBase="虚拟路径"。jsp页面的src虚拟路径下,文件名称"不要用中文"
    <Context path="/myvirtual/file" docBase="D:\ljx\test\" reloadable="true"></Context>
</Host>
(方法2)双击tomcat7服务器》切换 Modules窗口》点击 Add External Web Module...》3、Document base:点击Browse...(选择项目外资源路径)>Path:/myvirtual/file》点击确定

解决方案2、封装配置工具,相当于server.xml文件中,配置虚拟路径

package com.ljx.sj.util;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * 
* ****************************************************
*
* @Description: TODO(封装配置工具,相当于server.xml文件中,配置虚拟路径) 
*
* ****************************************************
 */
@Configuration
public abstract  class ConfigServerUtil implements WebMvcConfigurer {
	/**
	 * 
	* @Description: TODO(eclipse内置tomcat服务器的server.xml,下载tomcat服务器的server.xml通用。相当于server.xml文件中,配置虚拟路径)
	* @param registry
	* @Reamrk:ResourceLocations("....")为你选择的系统资源目录,需要在目录前加file:
	 */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/myvirtual/file2/**").addResourceLocations("file:D:/ljx/test/");
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值