views:

417

answers:

3

Using struts2 with jsp with standard struts tag libraries.

I'm trying to dynamically hide a DIV on page load, but still send it to the browser. This is so I can show it later via javascript.

The request objects one and two are not easily referenced via a jsp:usebean tag. (They are enums and cannot be instantiated) I tried using a combination of s:if tags or c:if tags and it just looks ugly.

<%
    String displayStr = "display: none;";
    if(request.getAttribute("one") != null || request.getAttribute("two") != null  ) {
        displayStr = "display: block;";
    }
 %>

<div id="next" style="<%=displayStr %>">

Any suggestions for a better way to do this?

A: 

Using JSTL:

   <c:set var="displayStr" value="display:none"/>
   <c:if test="${!empty one || !empty two}">
        <c:set var="displayStr" value="display:block"/>
   </c:if>
   <div id="next" style="${displayStr}">
Vincent Ramdhanie
A: 

Ugh, man, that was ugly. Let me swallow, and give you an alternative... Mmm... for example, you could write:

<div id="next" 
  style="display:<s:if test="divVisible">block</s:if><s:else>none</s:else>">

Or also

<s:if test="divVisible">
  <s:set name="divStyle" value="%{'display:block'}"/>
</s:if>
<s:else>
  <s:set name="divStyle" value="%{'display:none'}"/>
</s:else>

<div id="next" style="<s: property value="#divStyle">">

And then in your Action provide a isDivVisible() method which read the params "one" and "two" (not from the httpRequest!)

leonbloy
A: 

Just use plain EL in template text:

<div id="next" style="display: ${(!empty one || !empty two) ? 'block' : 'none'}">

Use of scriptlets is discouraged since over a decade. They should be all replaced by taglibs (Struts2, JSTL, etc) and EL. Whenever that's not possible, then the code logic almost certainly belongs in a real Java class, like a model object, a servlet (or Struts2 action), etc.

BalusC