Struts2中的路徑問題是根據action的路徑而不是jsp路徑來確定,所以盡量不要使用相對路徑。
以下為實例:
index.jsp

<%
@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Path</title>

</head>
<body>
<a href="path/path.action">路徑問題說明</a>

<br>

</body>
</html>
path.jsp

<%
@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts2_Path</title>

<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<base href="<%=basePath%>" /><!-- 使用base標簽指定本頁面所有鏈接的參照路徑 -->
</head>
<body>
Struts2中的路徑問題是根據action的路徑而不是jsp路徑來確定,所以盡量不要使用相對路徑。
<br>
<a href="index.jsp">index.jsp</a>
<br>

雖然可以用redirect方式解決,但redirect方式并非必要。
<br>
解決辦法非常簡單,統一使用絕對路徑。(在jsp中用request.getContextPath方式來獲取到webapp的路徑)
<br>或者使用myeclipse經常用的,指定basePath
<br>

</body>
</html>
PathAction.java
package com.bebig.struts2.path.action;

import com.opensymphony.xwork2.ActionSupport;


public class PathAction extends ActionSupport
{
@Override
public String execute()

{
return "path";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!-- Add packages here -->

<constant name="struts.devMode" value="true" />

<package name="front" namespace="/path" extends="struts-default">
<action name="path" class="com.bebig.struts2.path.action.PathAction">
<result name="path">
/path.jsp
</result>
</action>
</package>

</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
以下為實例:
index.jsp





































































































