Struts2_Path

路径问题说明:

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

index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <base href="<%=basePath%>"/>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>首页</title>
13 </head>
14 <body>
15 <h1>Hello World~~~</h1>
16 <a href="path/path.action">路径问题说明</a>
17 </body>
18 </html>

path.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
11 <title>Insert title here</title>
12 <base href="<%=basePath%>"/>
13 </head>
14 <body>
15 <h1>This is Path.jsp</h1>
16 struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。<br>
17 index.jsp<br>
18 虽然可以用rederect方式解决,但redirect方式并非必要。<br>
19 解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式拿到webapp的路径)<br>
20 或者使用myeclipse经常使用的,指定basePath<br>
21 <a href="index.jsp">index.jsp</a>
22 </body>
23 </html>

struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <constant name="struts.configuration.xml.reload" value="true"/>
 8     
 9     <!-- namespace 必须 "/" 开头 -->
10     <package name="front" namespace="/path" extends="struts-default">
11         <action name="path" class="com.bjsxt.struts2.front.action.PathAction">
12             <result name="path">/path.jsp</result>
13         </action>
14     </package>
15     
16 </struts>

链接: http://pan.baidu.com/s/1eSIcqLs 密码: serj

posted @ 2017-04-05 20:34  流年如水烟雨随风  阅读(351)  评论(0编辑  收藏  举报