JavaServer Pages (JSP) and JSTL - conditional attribute - how to

Hi,

I'm writing JSP document, so the jsp must be well formed.

I want to set the background of even and odd table rows by adding a class only on even rows (to avoid unnecessary source - network traffic).

Something like

if (even)

<tr class="even>

else

<tr>

How can this be done in JSP documents?

I've tried many approaches with JSTL,... but the "problem" is that condition can only be checked with <c:if> and <jsp:attribute> within <c:if> sets the attribute to <c:if>.

What am I missing?

Any ideas?

[607 byte] By [webfactorya] at [2007-11-26 23:11:17]
# 1

<tr class="<%= even ? "even" : "odd" %>">

Untested tho, I don't use JSP. Maybe you've to use ${even ? 'even' : 'odd'}. But you hopefully got the point.

BalusCa at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

this might work and is the current "solution"

but the point is to omit the

class="odd"

since it is not necessary and causes additional network traffic

even rows should be

<tr class="even">

odd rows should be

<tr>

(without any class)

webfactorya at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Well, it's a small change ..

<tr<%= even ? " class=even" : "" %>>

Remove the first > after tr. The dumb parser of this forum doesn't handle nested < and > correctly. But you got the point? Just move "class=" to the expression and keep the other hand empty ..

Message was edited by:

BalusC

BalusCa at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
this doesn't work in jsp document since as a well formed xml all attributes must have name and valuethe whole jsp expression is recognized as attribute name and the jsp document does not compile
webfactorya at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
try this:<tr <c:if test="${even}">class="even"</c:if>>
abc0xyza at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
I wanted to say without the closing ">" that comes right after "<tr"
abc0xyza at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
unfortunately this also does not compileexception says there must be an attribute specification after tr
webfactorya at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

you have to put these lines at the top of JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

And it must work because on my machine does. If not paste here the JSP fragment.

abc0xyza at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

as I already mentioned - I'm writing jsp documents

so here are the fragments

<?xml version="1.0" encoding="UTF-8" ?>

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:ebp="EBP.tld" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" version="2.0">

<jsp:directive.page session="false" language="java" contentType="application/xhtml+xml; charset=UTF-8"

pageEncoding="UTF-8" />

.....

<tr <c:if test="${!empty clazz}"><jsp:text> class="${clazz}"</jsp:text></c:if> >

........

and the (german) exception

com.ibm.ws.jsp.JspCoreException: /WEB-INF/jsp/umappe/auftragsTRs.jspx(39,9) /WEB-INF/jsp/umappe/auftragsTRs.jspx(39,9) Nach dem Elementtyp "tr" msen Attributspezifikationen, ">" oder "/>" folgen.

at com.ibm.ws.jsp.translator.document.Jsp2Dom.parseToDom(Jsp2Dom.java:334)

at com.ibm.ws.jsp.translator.document.Jsp2Dom.getJspDocument(Jsp2Dom.java:166)

at com.ibm.ws.jsp.translator.JspTranslator.<init>(JspTranslator.java:67)

webfactorya at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

try this:

<tr <c:if test="${!empty clazz}">class=<c:out value="${clazz}"/></c:if> >

Which are the implementations for JSTL ? (apache ?)

abc0xyza at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

I think there is Apache JSTL running in Websphere, but I don't think it is a JSTL problem.

The jsp source

<tr <c:if test="${!empty clazz}">class=<c:out value="${clazz}"/></c:if> >

is not well formed xml as it must be for jsp document so it can not be parsed as indicated also by part of stacktrace in error message:

at com.ibm.ws.jsp.translator.document.Jsp2Dom.parseToDom(Jsp2Dom.java:334)

at com.ibm.ws.jsp.translator.document.Jsp2Dom.getJspDocument(Jsp2Dom.java:166)

at com.ibm.ws.jsp.translator.JspTranslator.<init>(JspTranslator.java:67)

webfactorya at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

is not well formed xml as it must be for jsp document

so it can not be parsed as indicated also by part of

stacktrace in error message:

It is not necessary to be well formed xml so that JSP to work. Just do not put the constraint for well formed and it will compile and work.

abc0xyza at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13

maybe well formed would be like this:

<tr <c:if test="${!empty clazz}"><c:out value="class=${clazz}"/></c:if> >

abc0xyza at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14

tags within tags can not be well formed so the

<c:if in <tr breaks xml

I need to write the jsp as a jsp document. One point is that jsp documents do not produce any whitespace.

I believe there is no way to come around the

<tr class="odd">, although it is useless code (<tr> would be enough).

webfactorya at 2007-7-10 14:08:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15
You also can do this class alternation for rows from javascript (in onload function). So, you will have no traffic regarding this.
abc0xyza at 2007-7-21 19:23:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16
unfortunately not since the whole application is designed to work completely without javascript as well as with (the user can turn off javascript at any time)
webfactorya at 2007-7-21 19:23:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...