日期:2014-05-17  浏览次数:20882 次

求助 java web关于web.xml中welcome-file的配置
如何把非根目录的文件设置为welcome-file?
工程根目录为web,有一个文件夹为granted,内含一个index.jsp
把granted/index.jsp设置为welcome-file会造成
访问http://localhost:8080/web和访问http://localhost:8080/web/granted/index.jsp不同,后者的CSS、图片等路径都不对。求指导

------解决方案--------------------
css、图片等路径不对,那就应该在jsp里面采用绝对路径吧。
jsp页面头加上
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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%>">

然后在要引用的地方使用相对路径