如何在java中设置超链接url的上下文路径

时间:2011-10-28 09:51:33

标签: jsp hyperlink struts

 <html:link href="/dnweb/displayFindDeviceId.do" styleClass="small" onclick="appendHref(this)"> 
       <bean:message key="link.find"/>
 </html:link>

以上代码的上下文名称为“/ dnweb”。这是硬编码的。但我动态地需要它。

如果您将上下文名称更改为项目。必须自动更改。我该怎么做呢。

我的代码为

<html:link href="${pageContext.request.contextPath}/displayFindDeviceId.do"

但这是错误的。

4 个答案:

答案 0 :(得分:2)

您不需要pageContext。只需使用${request.contextPath}

即可

请注意,对于旧版本的JSP,您可能需要设置:

<%@ page isELIgnored="false" %> 

但是尝试升级servlet容器以支持更新版本。

答案 1 :(得分:0)

这是一个Struts1标签,不是吗?如果是这样,那么您应该使用actionpage属性来使用与上下文相关(甚至与模块相关的)URL。有关详细信息,请参阅http://struts.apache.org/1.x/struts-taglib/tagreference.html#html:link

答案 2 :(得分:0)

我使用html taglib标签设置上下文路径我可以使用scriptliet标签(如

)使用变量设置上下文路径

我在这里使用变量urlName来收集contextPath。

<jsp:useBean id="urlName" class="java.lang.String" scope="page" />
 <% urlName= request.getContextPath() + "/displayFindDeviceId.do"; %>
 <html:link href="<%=urlName %>" styleClass="small" onclick="appendHref(this)"> 
      <bean:message key="link.find"/>  
 </html:link>

根据此代码,它在我的localhost中正常工作。

感谢Bozho和JB。

答案 3 :(得分:0)

$ {pageContext.request.contextPath} 为我工作(春季版4.3.5.RELEASE和jstl版本1.2)。设置为ELIgnored =&#34; false&#34;仍然需要。

相关问题