Discussions

Web tier: servlets, JSP, Web frameworks: Dynamic JSP and Javascript.....Help!!

  1. Dynamic JSP and Javascript.....Help!! (9 messages)

    I am attemting to create a function call on the server side to be inserted into a JSP that contains the actual Javascript function to execute. The Javascript functions are used on the client side to initialize a menu and handle user actions. If I manually create the functions in a js file and include it in the page...

    <script language='JavaScript' TYPE="text/javascript" src="mymenu.js"> </script>

    ... The menus work as expected so I am sure that the client side code is working. What I need is the ability to generate the same contents that are in the js file through server side code and insert it into the page so that it is executed in the same manner.

    It seems to me that this is in fact possible, but I am unable to get this working. Each time I try I simply end up with either a blank area or all of the generated function call displaying as text. As a deadline is approaching I am at a loss and any assistance provided is greatly appreciated.

    Threaded Messages (9)

  2. Hmmm, I don't quite get what's the problem you are having. If I got it right, you need the JavaScript code to be generated, just as you're doing with your JSP. If that's the case, you can do several things. The simplest would be to map the url of the JavaScript resource to the JSP engine, so it's treated as any JSP, and then write the according JSP.
    In your web.xml:

    ...
    <servlet-mapping>
        <servlet-name>Your JSP Engine's Servlet</servlet-name>
        <url-pattern>/mymenu.js</url-pattern>
    </servlet-mapping>
    ...

    Then write a JSP file in the context root named mymenu.js
    Should work like a charm...
    Hope it helps...

    Regards,
    Martin
  3. Maybe this will help clairify my situation:

    Within my action class I generate the following statements:

    void buildMenuScript(){

     strMenuScript = "<script> MenuBar(150); ";
            
     for ( int x = 0; x < m_btnUserMenu.size(); x++ ){
       UserMenu tmpMnu = new UserMenu();
       tmpMnu = (UserMenu)m_btnUserMenu.get(x);
       strMenuScript += "MenuLabel('" + tmpMnu.caption + "');";
     }

     sideMenuActive += "endMenuBar(); </script>";
    }

    Result of call:

    <script>
       MenuBar(150);
          MenuLabel('menu1');
          MenuLabel('menu2');
          MenuLabel('menu3');
          MenuLabel('menu4');
       endMenuBar();
    </script>

    The string of calls from above is what I want to load into the JSP page and have it execute when it loads the page.

    Thanks,
    Shawn
  4. I see... Any particular reason to use an Action instead of JSP directly? Did you try using the jsp:include tag in your JSP?
  5. I must confess that I was thrown to the wolves on this project and I am learning on the fly. I am familure with the syntax for the technoligies used, but without expierence it is a hit and miss as to the implementation. I originally developed the javascript flyout menu code for use on an ASP site and the function calls to produce the menus were hardcoded with the menu values in a .js file that I used above. This project requires that users have the ability to add menus on the fly so I pull menu structures from an oracle db after they have been added. The functionality of the javascript occurs on the client side and rather than re-invent the wheel I am reusing the code for my JSP's. If there would be a better solution I am all ears, although I may need further assistance on the exact implementation.

    Thanks again,
    Shawn
  6. I think I get you now...
    You don't know where you should put that menu-generation code in the webapp? Is that it?
    Are you using and webapp framework, like Struts, or good old fashioned servlets and JSP?
  7. Yes I am unsure of where to add the code. When using ASP I used the <script....></script> tag to include the source javascript (.js) file within the head section of the page. There are two files included this way. One that sets up the initial configuration on page load and another that contains the functions for producing the flyout menus. No mater what I try I cannot get the files to load using this method in JSP. I have read many forums that all show this is the correct method for including javascript source files, but the files are not being included. I believe that this is the problem with the calls I am inserting to generate the menus on the fly as I get an error "...object expected". I am using struts in conjunction with tiles.
  8. Hi Shawn,

     i also have the simillar problem , can you pls share how you solved that issue

    thanks in advance
    thimi
  9. Try the below way

    <%! static StringBuffer javaScript = null;%>

    All your jsp-oracle manipulation and insert into the stringBuffer

    <%
    javaScript = new StringBuffer();
    javaScript.append("\nfunction name_of_the_function () {");
    javaScript.append("\nif() <condition");
             javaScript.append("\n}");
             javaScript.append("\n}");
    <html>
    <head>
    <title></title>

                <script language="javascript1.1" type="text/javascript">
    <--- And here call the stringbuffer --> <%=javaScript%>
    </script>
                       
    </head>

    it would work
  10. Include .js file in JSP[ Go to top ]

    I am also having an issue including a .js file into a JSP page. Has anyone else been successful?