【Struts2复习知识点四】-Path路径问题


struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。
虽然可以用redirect方式解决,但redirect方式并非必要。
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
或者使用myeclipse经常用的,指定basePath

struts.xml

View Code
 <constant name="struts.devMode" value="true" />
<package name="path" extends="struts-default" namespace="/path">
<action name="path" class="com.bjsxt.struts2.path.action.PathAction">
<result name="path">/path.jsp</result>
</action>
</package>

PathAction.java

View Code
public class PathAction {
public String execute() {
return "path";
}
}

 

index.jsp

View Code
<a href="path/path.action">路径问题说明</a>


访问路径: http://localhost:8080/project/index.jsp

path.jsp

 

View Code
<a href="index.jsp">index.jsp</a>

 访问路径:http://localhost:8080/project/path/path.action

 

 注:

在path.jsp中点击链接访问index.jsp时,找的路径是path/index.jsp    若写成/index.jsp则寻找的是http://localhost:8080/index.jsp

处理方法一:

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<a href="<%basePath%>index.jsp"></a>

处理方法二:

<head>
<base href="<%=basePath%>" />

</head>

<a href="index.jsp"></a>

posted @ 2012-02-21 23:18  濤叔  阅读(311)  评论(0编辑  收藏  举报