You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mark S Petrovic <ma...@petrovic.org> on 2005/11/14 19:17:15 UTC

Newbie help with Struts idiom: functional if/else tags

Good day.  I am new to Struts, and while I am coming up to speed, I'm
still struggling with what are surely common idioms.

In a JSP, I want to output, say, a <td> element body conditionally based
on a given bean property.

In pseudocode, I want

if bean.property == true
   print <td> X </td>
else
   print <td> Y </td>

I examined the Struts Logic tags, but found no if/else construct, and
I suspect there is a good reason for this.

Does Struts culture encourage me to somehow move the solution into the
bean itself, or is there a informed, stylish way to deal with this in
the JSP?

Thank you.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Mark S Petrovic <ma...@petrovic.org>.
Thank you for the reply.

Using, for example, <logic:equal> seems to solve only half the problem.
Using <logic:equal>, I can see my way clear to

<logic:equal name="feature" property="premium" value="true">
   <td>True</a>
</logic:equal>
<logic:notEqual name="feature" property="premium" value="true">
   <td>True</a>
</logic:notEqual>

which gives a crude if/else effect, but this is almost surely ill advised.
I think the same would apply to <c:if>, as I know of no "else" construct
that goes with either.

One could also perhaps use

<c:choose>
<c:when>
...
</c:when>
<c:otherwise>
...
</c:otherwise>
</c:choose>

which looks a bit better.

Thanks.

Mark

On 14Nov, Michael Jouravlev wrote:
> Use either Struts <logic:equal> or JSTL <c:if> or bare scriptlet. In
> your particular case you can make the decision in the bean itself. Do
> not print HTML tags from Java, this produces unmaintable and
> unportable code.
> 
> Michael.
> 
> On 11/14/05, Mark S Petrovic <ma...@petrovic.org> wrote:
> > Good day.  I am new to Struts, and while I am coming up to speed, I'm
> > still struggling with what are surely common idioms.
> >
> > In a JSP, I want to output, say, a <td> element body conditionally based
> > on a given bean property.
> >
> > In pseudocode, I want
> >
> > if bean.property == true
> >    print <td> X </td>
> > else
> >    print <td> Y </td>
> >
> > I examined the Struts Logic tags, but found no if/else construct, and
> > I suspect there is a good reason for this.
> >
> > Does Struts culture encourage me to somehow move the solution into the
> > bean itself, or is there a informed, stylish way to deal with this in
> > the JSP?
> >
> > Thank you.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org

-- 
Mark Petrovic
Pasadena, CA  USA

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Larry Meadors <lm...@apache.org>.
I like this:

<c:choose>
  <c:when test="${bean.property}">true</c:when>
</c:choose>

Larry

On 11/14/05, Michael Jouravlev <jm...@gmail.com> wrote:
> Use either Struts <logic:equal> or JSTL <c:if> or bare scriptlet. In
> your particular case you can make the decision in the bean itself. Do
> not print HTML tags from Java, this produces unmaintable and
> unportable code.
>
> Michael.
>
> On 11/14/05, Mark S Petrovic <ma...@petrovic.org> wrote:
> > Good day.  I am new to Struts, and while I am coming up to speed, I'm
> > still struggling with what are surely common idioms.
> >
> > In a JSP, I want to output, say, a <td> element body conditionally based
> > on a given bean property.
> >
> > In pseudocode, I want
> >
> > if bean.property == true
> >    print <td> X </td>
> > else
> >    print <td> Y </td>
> >
> > I examined the Struts Logic tags, but found no if/else construct, and
> > I suspect there is a good reason for this.
> >
> > Does Struts culture encourage me to somehow move the solution into the
> > bean itself, or is there a informed, stylish way to deal with this in
> > the JSP?
> >
> > Thank you.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Michael Jouravlev <jm...@gmail.com>.
Use either Struts <logic:equal> or JSTL <c:if> or bare scriptlet. In
your particular case you can make the decision in the bean itself. Do
not print HTML tags from Java, this produces unmaintable and
unportable code.

Michael.

On 11/14/05, Mark S Petrovic <ma...@petrovic.org> wrote:
> Good day.  I am new to Struts, and while I am coming up to speed, I'm
> still struggling with what are surely common idioms.
>
> In a JSP, I want to output, say, a <td> element body conditionally based
> on a given bean property.
>
> In pseudocode, I want
>
> if bean.property == true
>    print <td> X </td>
> else
>    print <td> Y </td>
>
> I examined the Struts Logic tags, but found no if/else construct, and
> I suspect there is a good reason for this.
>
> Does Struts culture encourage me to somehow move the solution into the
> bean itself, or is there a informed, stylish way to deal with this in
> the JSP?
>
> Thank you.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Mark S Petrovic <ma...@petrovic.org>.
On 15Nov, Mark Petrovic wrote:

> > >    <c:when test="${feature.premium == true}">
> > 
> > Should just be  <c:when test="${feature.premium}">

Sorry.  I failed to mention in my reply that I tried this, too, and it
made no difference in the html output.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Mark S Petrovic <ma...@petrovic.org>.
Wow.  Right on.  Using the <web-app> with additional attributes as shown
in the wiki entry below indeed solved the problem.

Thank you, and thanks to everyone else who patiently read my posts
and offered help.

Mark

On 15Nov, Rahul Akolkar wrote:
> On 11/15/05, Mark S Petrovic <ma...@petrovic.org> wrote:
> > I know I'll look back on this learning curve with a head-slapping
> > recollection, but until then, for posterity:
> >
> > 1.  If I remove the taglib directive in the JSP, the various <c:> tags
> > are copied directly into the html output, with the browser doing its
> > best to just ignore them.  The plaintext tag body content is actually
> > rendered, again, the result of the browser doing its best with what it
> > considers unknown markup.
> >
> > 2.  When I put the taglib directive back in, the JSP compiles and
> > subsequently does something with the <c:> tag directives, as the tags are
> > no longer copied into the resultant html and the default <c:otherwise>
> > always seems to execute.  Nor do I receive any exception output in the
> > browser or in the catalina.out logfile.
> >
> > Whatever is misconfigured in my app is not a completely fatal situation:
> > I just can't get at the scripting variables to do meaningful operations
> > on them.
> >
> <snip/>
> 
> Match JSTL to Servlet/JSP spec. You're using JSTL 1.1, and I suspect
> you don't have a Servlet 2.4 web.xml. Some details are here [1].
> 
> -Rahul
> 
> [1] http://wiki.apache.org/jakarta-taglibs/FrequentlyAskedQuestions
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org

-- 
Mark Petrovic
Pasadena, CA  USA

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Rahul Akolkar <ra...@gmail.com>.
On 11/15/05, Mark S Petrovic <ma...@petrovic.org> wrote:
> I know I'll look back on this learning curve with a head-slapping
> recollection, but until then, for posterity:
>
> 1.  If I remove the taglib directive in the JSP, the various <c:> tags
> are copied directly into the html output, with the browser doing its
> best to just ignore them.  The plaintext tag body content is actually
> rendered, again, the result of the browser doing its best with what it
> considers unknown markup.
>
> 2.  When I put the taglib directive back in, the JSP compiles and
> subsequently does something with the <c:> tag directives, as the tags are
> no longer copied into the resultant html and the default <c:otherwise>
> always seems to execute.  Nor do I receive any exception output in the
> browser or in the catalina.out logfile.
>
> Whatever is misconfigured in my app is not a completely fatal situation:
> I just can't get at the scripting variables to do meaningful operations
> on them.
>
<snip/>

Match JSTL to Servlet/JSP spec. You're using JSTL 1.1, and I suspect
you don't have a Servlet 2.4 web.xml. Some details are here [1].

-Rahul

[1] http://wiki.apache.org/jakarta-taglibs/FrequentlyAskedQuestions

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Dave Newton <ne...@pingsite.com>.
Mark S Petrovic wrote:

>Whatever is misconfigured in my app is not a completely fatal situation:
>I just can't get at the scripting variables to do meaningful operations
>on them.
>  
>
Can you set a String into scope:

request.setAttribute("foo", "bar");

then in a JSP do:

<c:out value="${foo}"/>

?

Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Mark S Petrovic <ma...@petrovic.org>.
I know I'll look back on this learning curve with a head-slapping
recollection, but until then, for posterity:

1.  If I remove the taglib directive in the JSP, the various <c:> tags
are copied directly into the html output, with the browser doing its
best to just ignore them.  The plaintext tag body content is actually
rendered, again, the result of the browser doing its best with what it
considers unknown markup.

2.  When I put the taglib directive back in, the JSP compiles and
subsequently does something with the <c:> tag directives, as the tags are
no longer copied into the resultant html and the default <c:otherwise>
always seems to execute.  Nor do I receive any exception output in the
browser or in the catalina.out logfile.

Whatever is misconfigured in my app is not a completely fatal situation:
I just can't get at the scripting variables to do meaningful operations
on them.

On 15Nov, Mark Petrovic wrote:
> On 15Nov, Dave Newton wrote:
> > Mark S Petrovic wrote:
> > 
> > >which isn't even iterating over the list items.  E.g, I get one line of
> > >html output for the <c:out value="${feature.premium}"/> tag, containing
> > >the string literal '${feature.premium}' in quotes - as if expansion of
> > >${feature.premium} is not taking place.
> > > 
> > >
> > You're using the JSP taglib directive? You have all the necessary JARs 
> > for JSTL? J2EE version your app server implements?
> 
> 1. The JSP has the following taglib directive
> 
> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
> 
> 2. In my WEB-INF/lib directory I have files and checksums 
> 
> 1677728891 20682 webapps/mx/WEB-INF/lib/jstl.jar
> 1935831951 393259 webapps/mx/WEB-INF/lib/standard.jar
> 
> from the TagLibs Standard 1.1.2 binary distrbution
> 
> 3. I'm using Apache Tomcat/5.5.12.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org

-- 
Mark Petrovic
Pasadena, CA  USA

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Mark S Petrovic <ma...@petrovic.org>.
On 15Nov, Dave Newton wrote:
> Mark S Petrovic wrote:
> 
> >which isn't even iterating over the list items.  E.g, I get one line of
> >html output for the <c:out value="${feature.premium}"/> tag, containing
> >the string literal '${feature.premium}' in quotes - as if expansion of
> >${feature.premium} is not taking place.
> > 
> >
> You're using the JSP taglib directive? You have all the necessary JARs 
> for JSTL? J2EE version your app server implements?

1. The JSP has the following taglib directive

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

2. In my WEB-INF/lib directory I have files and checksums 

1677728891 20682 webapps/mx/WEB-INF/lib/jstl.jar
1935831951 393259 webapps/mx/WEB-INF/lib/standard.jar

from the TagLibs Standard 1.1.2 binary distrbution

3. I'm using Apache Tomcat/5.5.12.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Dave Newton <ne...@pingsite.com>.
Mark S Petrovic wrote:

>which isn't even iterating over the list items.  E.g, I get one line of
>html output for the <c:out value="${feature.premium}"/> tag, containing
>the string literal '${feature.premium}' in quotes - as if expansion of
>${feature.premium} is not taking place.
>  
>
You're using the JSP taglib directive? You have all the necessary JARs 
for JSTL? J2EE version your app server implements?

Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Mark S Petrovic <ma...@petrovic.org>.
Thank you for the reply.

1.  I want to say yes, Feature is a proper JavaBean, with essentials

public class Feature {
  private boolean premium;
  public void setPremium(boolean b) { premium = b; }
  public boolean isPremium()        { return premium; }
}

and no overloaded methods.

"features" is populated thusly

ArrayList features = new ArrayList();
Feature f = new Feature();
f.setPremium(true); // e.g.
features.add(f);

and finally, in the Action.execute() method of the associated Struts
action

            request.setAttribute("features", features);
            return (mapping.findForward(target));

where the request.setAttribute() is the share-with-JSP-view technique
used by the Struts book I'm reading.

2.  Failing to get the <c:choose> to work within <logic:iterate> in
#1 above, I also tried a different tack:  using <c:forEach> instead of
<logic:iterate> to cycle through the list elements that <logic:iterate>
formerly treated:

For the same ArrayList "features", containing instances of bean "Feature"
as above and set with the same Struts Action.execute via

            request.setAttribute("features", features);

I attempt

<c:forEach var="feature" items="${requestScope.features}">
   <tr>
      <td>
         <c:choose>
            <c:when test="${feature.premium == true}">
               True
            </c:when>
            <c:otherwise>
               False
            </c:otherwise>
         </c:choose>
      </td>
      <td>
         <c:out value="${feature.premium}"/>
      </td>
   </tr>
</c:forEach>

which isn't even iterating over the list items.  E.g, I get one line of
html output for the <c:out value="${feature.premium}"/> tag, containing
the string literal '${feature.premium}' in quotes - as if expansion of
${feature.premium} is not taking place.

I know the issues I'm raising are very elementary, but I hope they help
someone else in the process.

Mark

On 14Nov, Wendy Smoak wrote:
> On 11/14/05, Mark S Petrovic <ma...@petrovic.org> wrote:
> 
> >    <c:when test="${feature.premium == true}">
> 
> Should just be  <c:when test="${feature.premium}">
> 
> Is 'feature' a proper JavaBean?  The types for get(is)/set methods
> match, no overloaded set methods, etc?
> 
> --
> Wendy
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org

-- 
Mark Petrovic
Pasadena, CA  USA

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Wendy Smoak <ws...@gmail.com>.
On 11/14/05, Mark S Petrovic <ma...@petrovic.org> wrote:

>    <c:when test="${feature.premium == true}">

Should just be  <c:when test="${feature.premium}">

Is 'feature' a proper JavaBean?  The types for get(is)/set methods
match, no overloaded set methods, etc?

--
Wendy

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Mark S Petrovic <ma...@petrovic.org>.
I'm still stumbling on some details.  

For

<logic:iterate id="feature" name="features">
   <tr>
      <td>
         <c:choose>
            <c:when test="${feature.premium == true}">
               <c:out value="true"/>
            </c:when>
            <c:otherwise>
               <c:out value="false"/>
            </c:otherwise>
         </c:choose>
      </td>
   </tr>
</logic:iterate>

fwiw, "features" is put in the request scope using 

            request.setAttribute("features", features);

in the Action that provides data to this JSP view.

The JSP prints false for cases where I know feature.isPremium() is true.
I suspect the 'feature' scripting variable has different meanings in the
two ways in which it is being used (logic:iterate tag and c:choose tag)

Help is appreciated, and I'm on my way to get modern books on both Struts
and JSPs.

On 14Nov, Craig McClanahan wrote:
> On 11/14/05, Mark S Petrovic <ma...@petrovic.org> wrote:
> >
> > Good day. I am new to Struts, and while I am coming up to speed, I'm
> > still struggling with what are surely common idioms.
> >
> > In a JSP, I want to output, say, a <td> element body conditionally based
> > on a given bean property.
> >
> > In pseudocode, I want
> >
> > if bean.property == true
> > print <td> X </td>
> > else
> > print <td> Y </td>
> >
> > I examined the Struts Logic tags, but found no if/else construct, and
> > I suspect there is a good reason for this.
> >
> > Does Struts culture encourage me to somehow move the solution into the
> > bean itself, or is there a informed, stylish way to deal with this in
> > the JSP?
> 
> 
> You can easily simulate if/else (or even if - else if - else if) chains with
> the JSTL <c:choose> tag, with nested <c:when> or <c:otherwise> tags. It's
> similar in spirit to using the "switch" statement in Java or C.
> 
> Craig
> 
> 
> Thank you.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >

-- 
Mark Petrovic
Pasadena, CA  USA

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie help with Struts idiom: functional if/else tags

Posted by Craig McClanahan <cr...@apache.org>.
On 11/14/05, Mark S Petrovic <ma...@petrovic.org> wrote:
>
> Good day. I am new to Struts, and while I am coming up to speed, I'm
> still struggling with what are surely common idioms.
>
> In a JSP, I want to output, say, a <td> element body conditionally based
> on a given bean property.
>
> In pseudocode, I want
>
> if bean.property == true
> print <td> X </td>
> else
> print <td> Y </td>
>
> I examined the Struts Logic tags, but found no if/else construct, and
> I suspect there is a good reason for this.
>
> Does Struts culture encourage me to somehow move the solution into the
> bean itself, or is there a informed, stylish way to deal with this in
> the JSP?


You can easily simulate if/else (or even if - else if - else if) chains with
the JSTL <c:choose> tag, with nested <c:when> or <c:otherwise> tags. It's
similar in spirit to using the "switch" statement in Java or C.

Craig


Thank you.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>