jspstruts2struts1

Check if a value is present in a content - Struts2


How can I check if a value is present in a table in Struts2? This is how it works in Struts1.

  <logic:present name="<%=Globals.ERROR_KEY%>">
        <table class="table">
          <tr>
            <td class="name" color="red">
    </logic:present>
              <html:errors />
    <logic:present name="<%=Globals.ERROR_KEY%>">
         </td>
      </tr>
     </table>
</logic:present>

Solution

  • You can use a method hasActionErrors() of ActionSupport class that could be checked in JSP if you need to wrap some content around <s:actionerror/>. You action should extend that class.

    Render action errors if they exists the specific layout of the rendering depends on the theme itself. Empty (null or blank string) errors will not be printed. The action error strings will be html escaped by default.


    <logic:present> equivalent in Struts2 is

    <s:if test="somekey != null && somekey != ''">
    

    where somekey is a variable in the valueStack, that has action scope. AFAIK the logic:present tag searching in all scopes, so you can add #attr prefix to the key.