Search code examples
javajspjsp-tags

Using JSP tags efficiently for null value from java code


<% Object domName = request.getAttribute("domainName");
   String documentationLink = UMRACM.getDomainDocumentationMap().get(domName);
%>

<td><a href="<%=documentationLink%>"target="_blank"
 id="domainName_<s:property value="#rowstatus.index"/>"><s:property value="domainName" /></a></td>

Well I have used this syntax to print the href as documentationLink but I think this is not an efficient way to do so ,Hence a little help required in using the `code.

Is there a better way to do the logic part of the code.

Also if I get

documentationLink = null

How shall I make the label unclickable


Solution

  • <% Object domName = request.getAttribute("domainName");
       String documentationLink = UMRACM.getDomainDocumentationMap().get(domName);
    %>
    

    Do this ^^^ to your Server Side. and put your retrieved documentationLink to either session or request scope. Like this:

       Object domName = request.getAttribute("domainName");
       String documentationLink = UMRACM.getDomainDocumentationMap().get(domName);
       request.setAttribute("documentationLink",documentationLink);
    

    As I can see, you are using Struts-tags. So remove your scriptlets and try to use tags used in Struts2. Like this,

    <td>
    <s:if test="%{#request.documentationLink != null}">
    <a href="<s:property value="#request.documentationLink"/>"target="_blank"
     id="domainName_<s:property value="#rowstatus.index"/>"><s:property value="domainName" />
    </a>
    </s:if>
    <s:else>
    <s:property value="domainName" />
    </s:else>
    </td>