This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help please  RSS feed

 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using an if taglib and my tag has a boolean expression
<if:IfTrue cond="2==2" >
check
</if:IfTrue>
Format is
cond="true", then the next statement will be evaluated
Am I missing something here ?
Why my expression is not getting evaluated to true >?
Please correct my code..
Thanks
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the JSP container will treat "2==2" as a String value. You need to use a request-time expression for the value of your attribute, i.e. "<%= 2==2 %>".
For this to work, you also need to allow rt expressions for that attribute in the tag library descriptor file (the <rtexprvalue> tag).
Cheers
Simon
 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Simon,
I shall try that and let you know
Also,
I am having another problem with a tag that I wrote:

This is my tag tld:<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>example</shortname>
<uri>/WEB-INF/example.tld</uri>
<info>
A description of the display tag library goes here...
</info>

<tag>
<name>hello</name>
<tagclass>example.Hello</tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>name</name>
<required>true</required>
<rtexpvalue>true</rtexpvalue>
</attribute>
</tag>
</taglib>
My tag class:
package example;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* This is a simple tag example to show how content is added to the
* output stream when a tag is encountered in a JSP page.
*/
public class Hello extends TagSupport {
private String name=null;
/**
* Getter/Setter for the attribute name as defined in the tld file
* for this tag
*/
public void setName(String value){
System.out.println("inside setter"+value+"is the value from jsp");
name = value;
}
public String getName(){
System.out.println("inside getter");
return(name);
}
/**
* doStartTag is called by the JSP container when the tag is encountered
*/
public int doStartTag() {

try {
JspWriter out = pageContext.getOut();
out.println("This is name'"+name+"'");
out.println("<table border='\1\'>");
if (name != null)
out.println("<tr><td> Hello " + name + " </td></tr>");
else
out.println("<tr><td> Hello World </td></tr>");
} catch (Exception ex) {
throw new Error("All is not well in the world.");
}
// Must return SKIP_BODY because we are not supporting a body for this
// tag.
return SKIP_BODY;
}
/**
* doEndTag is called by the JSP container when the tag is closed
*/
}
My jsp has
Vector v = new java.util.Vector();
v.addElement("first");
v.addElement("second");
v.addElement("third");
v.addElement("Test");
String idx = (String) v.elementAt(0);
<samples:hello name="<%=idx%>">
Enter here
</samples:hello>

Now this tag, is not evaluating the expression, <%=idx%>

But if I hardcore a string inside the tag, it is working also
What is the problem here ?
Thanks
 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Simon,
if(2==2) works when your suggestion is applied/
But I am trying to get 2 vars dynamically and trying to compare them
like this<do:forLoop var="i" from="0" to="<%=v.size()-1%>" step="1">
<br>Element: <%=v.elementAt(i.intValue())%>
<do:forLoop var="j" from="0" to="<%=v1.size()-1%>" step="1">

<if:IfTrue cond="<%=i==j%>" >
<br>Second Element: <%=v1.elementAt(j.intValue())%>
</if:IfTrue>
</do:forLoop>
</do:forLoop>
But now the condition is not being evaluated,, any idea ?
Thanks for your help
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since only objects (not primitives) can be introduced as scripting variables into the page, the expression i==j is trying to evaluate equality between two object references, probably (guessing here) of type java.lang.Integer which is why it never evaluates to true.
Therefore, you could use either of the following as your attribute value:
  • "<%= i.intValue() == j.intValue() %>"
  • "<%= i.equals(j) %>"


  • Let us know if that works - I'm off to bed now!
    Cheers
    Simon
     
    Shreya Menon
    Ranch Hand
    Posts: 285
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Simon
    It worked
    Also, I have another problem, may be tomorrow if you have time, could you please check that and let me know
    Thanks so much and have a good night
    Maya
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!