Link to home
Start Free TrialLog in
Avatar of sdesar
sdesar

asked on

Need Syntax help on converting JSP - <a> tag into a Java function?

Hello,

I have a JSP page that displays treeview using a tree tag lib.

I need to recursively call this tree view so I am placing it in the JAVA recurseNode function.
The error that I have is
JSPProperties.java:220: illegal character
when I am doing :-
 resultBuffer.append("<tree:addNode code='<%="<a href=\\"http://www.yahoo.com\\">"

the entire resultBuffer.append string.

Would you please help me with the syntax ?

here is the Java syntax in the function recurseNode  that is NOT working ( **I need your help to fix it)-

 //print out <tree with link  ** I get a illegal character error .. on these lines
//  <tree:addNode code='<%="<a href=\\"http://www.yahoo.com\\">"+hChildNode.getName()+"</a>"%>'/>

            resultBuffer.append("<tree:addNode code='<%="<a href=\\"http://www.yahoo.com\\">"
            resultBuffer.append(thisNode.getName());
            resultBuffer.append("</a>"%>'/>");

        } else {
            //print without link
            resultBuffer.append("<tree:addNode code='");
            resultBuffer.append(thisNode.getName());
            resultBuffer.append("'>");
        }



And here is the JSP syntax that is working on the JSP :

<tree:addNode code="<%=gChildNode.getName() %>">
<%
synchronized (gChildNode) {
      final Enumeration hChildren = gChildNode.children();

while (hChildren.hasMoreElements())
{
     final Node hChildNode = (Node)(hChildren.nextElement());
%>
<%-- this  a tag works fine  --%>
<tree:addNode code='<%="<a href=\\"http://www.yahoo.com\\">"+hChildNode.getName()+"</a>"%>'/>




- Awaiting a response

THanks
Avatar of kennethxu
kennethxu

use single \ instead of double \\
>> resultBuffer.append("<tree:addNode code='<%="<a href=\\"http://www.yahoo.com\\">"
resultBuffer.append("<tree:addNode code='<%="<a href=\"http://www.yahoo.com\">"
Hi,
I am not sure what string you are expecting at the end from yours StringBuffer object but I think that the contactination of the string is not correct. Could you please send us the resulted string which you want? In any case try this -

resultBuffer.append("<tree:addNode code=\"<a href='http://www.yahoo.com'>"+hChildNode.getName()+"</a>\"");


Kindly reply back. I hope it helps you.
Thx

Avatar of sdesar

ASKER

When the code runs thisNode.getName() gets passed as a string, I need it to output some value like 'employee' etc...

I tired to pass it as resultBuffer.append("+ thisNode.getName()");  // does not work
or
resultBuffer.append(thisNode.getName());  // does not work

BTW - seems like just one '/' works but I won't know for sure until I see the JSP running...

Here is the code below--


// in JSP the tree tag looks like the comment below
            //<tree:addNode code='<%="<a href=\\"http://www.yahoo.com\\">"+hChildNode.getName()+"</a>"%>'/>

            System.out.println("if loop");
           resultBuffer.append("<tree:addNode code='<%=\"<a href=http://www.yahoo.com\">");
           resultBuffer.append("thisNode.getName()");
           resultBuffer.append("</a>%>'/>");
        //    System.out.println(resultBuffer);


Thanks all for advise

Awaiting a response
the <% thing won't work as a sting in the function. can you post the full code
Avatar of sdesar

ASKER

Here is the code



public String recurseNode(DbNode thisNode, int level) {
   //  level = 0;
     final int maxLevel = 4;

        final StringBuffer resultBuffer = new StringBuffer();

        final NodeClassFilter filter = new NodeClassFilter();
        filter.accept(DefaultNode.class);
        filter.reject(SystemFolderNode.class);
        filter.reject(SystemFileNode.class);
        filter.reject(HistoryContainerNode.class);
        filter.reject(DeletedItemsFolderNode.class);

        System.out.println("I am in recurseNode");

        //DO STUFF HERE
        if (level == maxLevel) {
            //print out <tree with link

            // in JSP the tree tag looks like the comment below
            //<tree:addNode code='<%="<a href=\\"http://www.yahoo.com\\">"+hChildNode.getName()+"</a>"%>'/>

            System.out.println("if loop");
           resultBuffer.append("<tree:addNode code='<%=\"<a href=http://www.yahoo.com\">");
           resultBuffer.append("+ thisNode.getName()");
           resultBuffer.append("</a>%>'/>");
        //    System.out.println(resultBuffer);

        } else {
            //print without link
             System.out.println("else loop");
            resultBuffer.append("<tree:addNode code='");
            resultBuffer.append("+ thisNode.getName()");
            resultBuffer.append("'>");
          //  System.out.println(resultBuffer);
        }

        synchronized (thisNode) {
            try {

                if ((thisNode.getChildCount() > 1) && (level < maxLevel)) {
                    final Enumeration thisNodeChildren = thisNode.children();

                    while (thisNodeChildren.hasMoreElements()) {
                        final Node thisNodeChild = (Node)(thisNodeChildren.nextElement());

                          System.out.println("I am in synchronized recurseNode");
                        if ((thisNodeChild instanceof DbNode) && (filter.accept(thisNode))) {

                            resultBuffer.append(recurseNode((DbNode)thisNodeChild, level+1));
                        }  // end inner if
                    }  // end while
                }  // end outer if
                 System.out.println("end tag");
           //     resultBuffer.append("</tree:addNode>");
                 System.out.println(resultBuffer);
                return resultBuffer.toString();
            } catch (CommandFailedException cfe) {
                Log.write(cfe);
                return null;
            }
        }  // synchronized

    }  // end recurseNode

Avatar of sdesar

ASKER

This is how I call the function in the JSP -
GetTree.jsp
------------

 <%@ page import = "src.abtest.*"
 
%>

<%@ taglib uri="taglib_tree.tld" prefix="tree" %>

<%
 ProjectNode projectNode=null;

projectNode= BmxServletUtilities.getProject(session);
JSPProperties JP = new JSPProperties();

%>
<a href="http://www"> hello </a>
<tree:createTree treePicture="tree.gif" nodePicture="leaf.gif" openPicture="open.gif" dhtml="true">
 <%   JP.recurseNode(projectNode, 0); %>
</tree:createTree>


The JSP calls the recurseNode and I see it running in the logs with the System.out.println('comments').

But I don't see any output in the JSP.

I suspect its this code that is not working correctly since the syntax is not right -


   System.out.println("if loop");
           resultBuffer.append("<tree:addNode code='<%=\"<a href=http://www.yahoo.com\">");
           resultBuffer.append("+ thisNode.getName()");
           resultBuffer.append("</a>%>'/>");
        //    System.out.println(resultBuffer);

        } else {
            //print without link
             System.out.println("else loop");
            resultBuffer.append("<tree:addNode code='");
            resultBuffer.append("+ thisNode.getName()");
            resultBuffer.append("'>");
          //  System.out.println(resultBuffer);
        }


The logs just diplay the data like this -



 I am in recurseNode
    if loop
        <tree:addNode code='<%="<a
            href=http://www.yahoo.com">thisNode.getName()</a>%>'/>
            I am in synchronized recurseNode
            I am in synchronized recurseNode
            I am in synchronized recurseNode
            I am in synchronized recurseNode
            I am in recurseNode
            I am in synchronized recurseNode
            I am in recurseNode
            I am in synchronized recurseNode
            I am in recurseNode
            I am in synchronized recurseNode
            I am in recurseNode
            if loop
                <tree:addNode code='<%="<a
                    href=http://www.yahoo.com">thisNode.getName()</a>%>'/>



Thanks for helping -
Awaitng a response!
Avatar of sdesar

ASKER

I think I am pretty close /.. I see the data in the LOGS BUT NO data in the HTML/JSP Page.. Eargerly awaiting a reponse..

Thanks for your advise.
I implemented your suggestions.
I don't see the data in HTML/JSP page.  
However, when I view the logs all the data is being displayed in the logs.

Awaiting Suggestions!

In the JSP I tried to add -
<%= JP.recurseNode(projectNode, 0) %>

or

<% JP.recurseNode(projectNode, 0); %>

Both of these output to the logs and NOT to the web browser.

This line

<%= JP.recurseNode(projectNode, 0); %>   // with the semicolon produces an error
/usr/local/tomcat/jakarta-tomcat-5.0.18-abrain/work/Catalina/localhost/browseit/org/apache/jsp/GetTree_jsp.java:91: ')' expected
          out.print( JP.recurseNode(projectNode, 0); );




// JSPProperties.java

 public String recurseNode(DbNode thisNode, int level) {
        final int maxLevel = 4;
        final StringBuffer resultBuffer = new StringBuffer();
        final NodeClassFilter filter = new NodeClassFilter();
        filter.accept(DefaultNode.class);
        filter.reject(SystemFolderNode.class);
        filter.reject(SystemFileNode.class);
        filter.reject(HistoryContainerNode.class);
        filter.reject(DeletedItemsFolderNode.class);
         // DbNode dbnode=(DbNode)thisNode.findNode(path);
        System.out.println("I am in recurseNode");
        // System.out.println("Node is: " + thisNode.getDisplayName());

        //DO STUFF HERE
        if (level == maxLevel) {
            //print out <tree with link

            // in JSP the tree tag looks like the comment below
            //<tree:addNode code='<%="<a href=\\"http://www.yahoo.com\\">"+hChildNode.getName()+"</a>"%>'/>

            System.out.println("if loop");
           resultBuffer.append("<tree:addNode code='<%=\"<a href=http://www.yahoo.com\">");
           try {
                resultBuffer.append(thisNode.getName());
           } catch (CommandFailedException cfe) {
                Log.write(cfe);
           }
           resultBuffer.append("</a>%>'/>");
            System.out.println(resultBuffer);
        } else {
            //print without link
             System.out.println("else loop");
            resultBuffer.append("<tree:addNode code='");
            try {
                resultBuffer.append(thisNode.getName());
            } catch (CommandFailedException cfe) {
                Log.write(cfe);
            }

            resultBuffer.append("'>");
            System.out.println(resultBuffer);
        }
        synchronized (thisNode) {
            try {
                if ((thisNode.getChildCount() > 1) && (level < maxLevel)) {
                    final Enumeration thisNodeChildren = thisNode.children();
                    while (thisNodeChildren.hasMoreElements()) {
                        final Node thisNodeChild = (Node)(thisNodeChildren.nextElement());
                          System.out.println("I am in synchronized recurseNode");
                        if ((thisNodeChild instanceof DbNode) && (filter.accept(thisNode))) {
                            resultBuffer.append(recurseNode((DbNode)thisNodeChild, level+1));
                        }  // end inner if
                    }  // end while
                }  // end outer if
                 System.out.println("end tag");
               resultBuffer.append("</tree:addNode>");
           //      System.out.println(resultBuffer);
                return resultBuffer.toString();
            } catch (CommandFailedException cfe) {
                net.common.Log.write(cfe);
                return null;
            }
        }  // synchronized
    }  // end recurseNode




//GetTree.jsp

  <%@ page import = "src.abtest.*"
  import="java.util.*"
 
%>

<%@ taglib uri="taglib_tree.tld" prefix="tree" %>

<%
 ProjectNode projectNode=null;

projectNode= BmxServletUtilities.getProject(session);
JSPProperties JP = new JSPProperties();

%>
<a href="http://www"> hello </a>
<tree:createTree treePicture="tree.gif" nodePicture="leaf.gif" openPicture="open.gif" dhtml="true">
<%= JP.recurseNode(projectNode, 0) %>
</tree:createTree>




//Snippet of the LOGS =



I am in synchronized recurseNode
end tag
I am in synchronized recurseNode
I am in recurseNode
else loop
<tree:addNode code='beths_garage'>
I am in synchronized recurseNode
I am in recurseNode
if loop
<tree:addNode code='<%="<a href=http://www.yahoo.com">locations143-ld.jpg</a>%>'/>
end tag
I am in synchronized recurseNode
I am in recurseNode
if loop
<tree:addNode code='<%="<a href=http://www.yahoo.com">locations24-rgh-ld.jpg</a>%>'/>
end tag
Avatar of sdesar

ASKER

Please help..

I think all the code is fine .. and I see the data being outputted to catalina.out log file and I want the data to output to JSP .
What is the syntax to tell the JAVA code to output to JSP instead of using resultBuffer.append  I need some JSP Writer???


Eagerly awaiting suggeestions...
Avatar of sdesar

ASKER

The suggestions were good, but it did not help me with the treeview taglib I used from http://www.servletsuite.com/

The tree view tag lib accepts a  treeBean object, so I had to add a new parameter TreeBean parent


TreeBean node1 = null;
node1 = new TreeBean();
node1.setCode(thisNode.getName();

And in the JSP

TeeBean root = new TreeBean();
root.setCode("employee");
JP.recurseNode(root, projectNode, 0);

etc...

Sincere thanks for your kind efforts with this question, but since the suggestion did not help answer the question could you please refund the points.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial