一般访问某个jsp程序如下,假设index.jsp在WebContent里面
<a href="index.jsp">
若不使用structs框架,则可以访问到,但是使用了该框架,就不能这么访问了
首先structs Action 访问举个例子,在浏览器输入http://localhost:8080/project-name/packge-namespace/action-name,若是我在该链接里的某个jsp页面里 访问另一个jsp
<a href="index.jsp">
则访问路径为:http://localhost:8080/project/name/packge-namespace/index.jsp, 两个jsp访问的package-namespace是一样的,但是是访问不到index.jsp的,这说明
structs框架的路径访问 不是根据资源的位置路径来访问,而是根据Action的路径来访问,因此
Structs 路径访问资源,一定要采用绝对路径访问,最后的访问路径应该类似这样:http://localhost:8080/project-name/index.jsp
贴出一段路径访问demo
<%@ page language="java" contentType="text/html; charset=UTF-8"
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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index.jsp</title>
</head>
<base href="<%=basePath%>">
<body>
index index index index index index aaaaa
<a href="Hello.jsp">路径问题说明</a>
</body>
</html>期中,调用
<base href="<%=basePath%>">可使得整个页面的所有路径自动加上basePath(basePath为http://localhost:8080/project-name),最后的访问路径类似这样:http://localhost:8080/project-name/index.jsp
本文探讨了在Struts框架环境下JSP页面之间的正确路径访问方式。当使用Struts框架时,若要从一个JSP页面访问另一个JSP页面,必须使用绝对路径而非相对路径。文章通过示例解释了如何设置正确的basePath,并提供了实现这一功能的代码片段。

1109

被折叠的 条评论
为什么被折叠?



