struts2中的路徑問題是根據action的路徑而不是jsp路徑來確定,所以盡量不要使用相對路徑。
雖然可以用redirect方式解決,但redirect方式并非必要。
解決辦法非常簡單,統一使用絕對路徑。(在jsp中用request.getContextpath方式來拿到webapp的路徑)
或者使用myeclipse經常用的,指定basePath
例子:
<?xml version="1.0" encoding="GB18030" ?> <%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%><%@taglib uri="/struts-tags" prefix="s" %><%//這里的path是得到webapp的名字,如果我們的webapp名字是struts_0400_path//那么path就是struts_0400_path//basePath包含了path內容,他是全路徑: http://localhost:1000/struts2_0400_pahtString path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>說明:<base href="<%=basePath%>" />是指定根路徑。 <base href="<%=basePath%>" /><meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>Insert title here</title> </head> <body>
說明:在這里我們使用的鏈接是index.jsp,但是因為在<head>中我們定義了<base href="<%=basePath%>" />
所以在這里的鏈接其實是:http://localhost:1000/struts2_0400_path/index.jsp?路徑
這就是<base href="<%=basePath%>" />的好處之所在了。
<a href="index.jsp">index.jsp</a>