struts2(struts-2.0.14)中的路径问题:
路径是根据action路径而不是jsp路径来确定的,所以尽量不要使用相对路径;
解决方法:统一使用项目的绝对路径!
即在JSP中使用request.ContextRoot方式来拿到项目的路径;
MyEclipse经常使用的basePath也行;
来个例子:
未使用绝对路径由在同一目录下的index.jsp跳转到hello.jsp,则在index.jsp中:
<body>
<a href="Hello.jsp">go to Struts2!</a> <br>
</body>
对应的struts.xml文件:
<action name="hello">
<result>
/index.jsp
</result>
</action>
<action name="path">
<result name="path">
/Hello.jsp
</result>
</action>
跳转结果是:404notfound;
将index.jsp中的跳转链接改为:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<head>title</head>
<body>
<a href=<%=basePath%>"Hello.jsp">go to Struts2!</a> <br>
</body>
就可以正常显示了,
不过我在实际敲的过程中发现;在MyEclipse8.5中,struts2-2.2.1可以不用绝对路径而正常跳转,估计是struts版本的一个改进吧.
Struts2路径问题解析
本文探讨了Struts2框架中路径配置的问题,指出在JSP页面中使用相对路径可能导致404错误,并提供了解决方案,即通过request获取项目的绝对路径。
964

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



