structs2 中链接引用的绝对路径
在 structs2 框架下, 每个jsp在进行<a href=”..”></a>引用时, href中地址都是相对于当前action的, 这样在项目架构进行改变时,引用的路径也要改变,这样很不方便.
因此, 推荐在使用路径时采用绝对路径:
需要在jsp文件中加入:
[html]
- <% String path=request.getContextPath();
- String basepath=request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;%>
这样的 basepath就代表了当前项目的根路径.
如果我想在访问当前项目 path/ 下的 path.action 只需要如下代码:
[html]
- <%@ page language=”java” contentType=”text/html; charset=GB18030″
- pageEncoding=”GB18030″%>
- <strong> <% String path=request.getContextPath();
- String basepath=request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;%>
- </strong><!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=GB18030″>
- <title>Insert title here</title>
- </head>
- <body>
- <a href<strong>=”<%=basepath %></strong>path/path.action”>路径问题</a>
- </body>
- </html>