Search code examples
jsphyperlinkstruts

How to set context path to the hyperlink url in java


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

The above code having context name as "/dnweb". This is given as hardcoded. But i need it as dynamically.

If you changed the context name to your project. Automatically it has to be changed. How can i do for this.

i have the code as

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

But this is giving an error.


Solution

  • i set the context path using html taglib tag i can set the context path using a variable by using scriptliet tags like

    i took variable urlName to collect contextPath here.

    <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>
    

    As per this code it is working fine in my localhost.

    Thanks to Bozho and JB.