【问题标题】:How to set context path to the hyperlink url in java如何在java中设置超链接url的上下文路径
【发布时间】:2018-06-03 01:41:48
【问题描述】:
 <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"

但这会报错。

【问题讨论】:

标签: jsp hyperlink struts


【解决方案1】:

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

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

<%@ page isELIgnored="false" %> 

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

【讨论】:

【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我使用 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>
    

    根据此代码,它在我的本地主机中运行良好。

    感谢 Bozho 和 JB。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2016-09-29
        • 2017-04-22
        • 2019-01-20
        • 2019-08-22
        • 1970-01-01
        • 2015-09-09
        • 1970-01-01
        • 2015-05-26
        • 2011-12-18
        相关资源
        最近更新 更多