structs2 中链接引用的绝对路径

来源:互联网 发布:身份证伪造软件app 编辑:程序博客网 时间:2024/06/05 02:40

在 structs2 框架下, 每个jsp在进行<a href=".."></a>引用时,  href中地址都是相对于当前action的, 这样在项目架构进行改变时,引用的路径也要改变,这样很不方便.

因此, 推荐在使用路径时采用绝对路径:

需要在jsp文件中加入:

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


 

这样的 basepath就代表了当前项目的根路径.

如果我想在访问当前项目 path/ 下的 path.action 只需要如下代码:

<%@ page language="java" contentType="text/html; charset=GB18030"    pageEncoding="GB18030"%>    <% 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=GB18030"><title>Insert title here</title></head><body><a href="<%=basepath %>path/path.action">路径问题</a></body></html>


 

0 0
原创粉丝点击