使用MyEclipse开发了一个项目,Web Context-root设置的是/,部署到Tomcat的webapps\ROOT目录。
访问时使用相对路径,例如:<IMG height=37 src="/images/cms/indexpic/index_20.jpg" width=341>
现在利用这个项目复制出一个新的项目,Web Context-root设置成/new,部署到Tomcat的webapps\new目录,这时使用相对路径访问出现问题,之前的/images/cms/indexpic/index_20.jpg需要改为new/images/cms/indexpic/index_20.jpg才能访问到,但项目规模较大,不可能在每个相对路径前面都加上<%=request.getContextPath()%>,有没有办法通过配置的方式解决这个问题?

解决方案 »

  1.   


    没什么解决办法,路径不对肯定访问不了
    除非装两个tomcat,端口不同
      

  2.   

    没有更好办法,不过你可以在jsp页面的<head></head>标签前加上<base>标签中设置一下路径
      

  3.   

    <%@ 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>My JSP 'index.jsp' starting page</title>
    <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>