View / logic taglib


Missing Logic

I have an object in the session that if it is not there I want to display
and error msg to the user. My error code is as follows:
 
<logic:notPresent name="VendorResourceList" scope="session">
<font color="red">
<bean:message key="error.fatal" />
</font>
</logic:notPresent>

I would think that the above code would show the message if the
"VendorResourceList" object is not found in the session but the error msg
 is always displayed even though the below code snippet works.
 
 <TABLE>
 <TR>
 <TD>
 <bean:message key="prompt.vendorNumber"/>
 </TD>
 <TD>
 <logic:present name="VendorResourceList" scope="session">
 I am here
 </logic:present >
 </TD>
 
 Any help in understanding this is appreciated.

[Ted Husted] Be sure you've included the logic tld. The "I am here" would display regardless, since the browsers ignore tags they don't understand. 


Present?

How do I use the logic tags to check for a null property value on a bean.  I've tried using <logic:notEqual property="myBean.item1" value="null"> and > <logic:present name="myBean.item1> but to no avail.

[Ted Husted] Try <logic:present name="myBean" property="item1"> 


Alternating colours with logic:iterate

"I'm using the logic:iterate tag to render a table. Now I need to show alternating colours for odd and even table rows. How do I achieve this effect together with the logic:iterate tag?"

[Greg Reddin]:

String LIGHT_COLOR = "LightColor";
String currentColor = DARK_COLOR;
int i = 0;
%>
<struts:enumerate id="searchRow" 
name="searchForm" property="SearchResults"> 
<%
if ( i % 2 == 0)
{
currentColor = DARK_COLOR;
}
else
{
currentColor = LIGHT_COLOR;
}
i++;
%>
<TR ALIGN="center" class="<%=currentColor %>">

[Scott Sayles] What you could do is create a color alternating bean. I've made one for similar uses. Here's how mine is layed out:

--ColorAlternator--
public String getNext()
public String getCurrent()
public void setColor1(String color1)
public void setColor2(String color2)
public void setColor3(String color3)
public int getNumberOfColors()
public void setNumberOfColors(int numOfColors) // should be either 2 or 3
-------------------

You just implement it so that each call to next sets the current color to the next one and returns it. Actually, this is really just a String alternator but the intent is for color names. I suppose that I could have used some kind of circular list and then enabled
it to handle any number of colors, but my assumption was that you probably never have a use for more than 3 colors. It makes the implementation pretty simple.

Then you can use this bean whenever you want. For example:

----- JSP page -------------

<jsp:useBean id="color" class="ColorAlternator"/>
<jsp:setProperty name="color" property="color1" value="white"/>
<jsp:setProperty name="color" property="color2" value="oldlace"/>

<table>
<logic:iterate ....>
<tr bgcolor='<jsp:getProperty name="color" property="next">'> 
<td>content</td>
</tr>
</logic:iterate>
</table>

[Bob Sullivan] You might want to check out JSPTags.com's Pager tag library. They have about 4 different flavors of displays to allow just sort of a presentation effect. I haven't used it in anything real, so I can't speak to the quality or robustness (but it sure looks pretty :)

[Julia Reynolds ] Here's what I have done:

out.println("<table cellspacing=0 cellpadding=0 width=600>");
int row = 1;
String color = "";
while(rec.next())
{
if((row%2)==0)
{
color = "white";
}
else
color = "DDDDDD";

out.println("<tr bgcolor=" + color + "><td ><a href=" +
rec.getString("URL") + "" + " target=\"_blank\"" + ">" +
rec.getString("Description") + "</a></td><td> Category: "
+ rec.getString("CatName") + "<br></td></tr>");
row++;
}
out.println("</table>");

It's not beautiful, but it works.

[Proetel, Ingo] I'm creating the effect like this:

<% int count=0; %>
<struts:iterate id="row" name="currentReport" property="data" >
<tr BGCOLOR='<%= (count++ % 2==0 ?"#FFFFAA":"#EEEEEE") %>' >
<td> <%= count %> </td>
<struts:iterate id="item" collection="<%= row %>" >
<td nowrap align=MIDDLE > <%=(item).toString() %> </td>
</struts:iterate>
</tr> 
</struts:iterate>

[ Niall Pemberton] I have a RowTag that does this, it generates <tr> elements and you can
specify either oddColor/evenColor attributes to generate bgcolor= attributes or oddStyle/evenStyle parameters to generate class= attributes for CSS.

So the jsp looks like this:

<table>
<logic:iterate id=".." name=".." property="..">
<util:row oddColor="#C0C0C0" evenColor="#FF0000">
<td>.....</td>
<td>.....</td>
</util:row>
</logic:iterate>
</table>

You can either specify both odd and even or just one of them.

It's available on the More About Struts Resources page  if you’re interested.