Matt Raible wrote:
Message
I'd put it into the request.
 
request.setAttribute("repository", repository);
 
Note that you'll have to do this every time you want to view the page since the repository will disappear once the page is done loading.  You could also put it into the user's session and then it would stay there for duration of their session - however long you have that set to.
 
Matt

Matt,

I found a different, maybe less elegant solution to that:

In the Struts-Action I put the stuf f in the application context:

        session.getServletContext().setAttribute(MenuRepository.MENU_REPOSITORY_KEY, repository);

In the Jsp I take it from the application context:

    <%
    pageContext.setAttribute("repository", (MenuRepository)application.getAttribute(MenuRepository.MENU_REPOSITORY_KEY));
    %>
At least this works fine but may not be efficient.

What I do not understand: (Just for a better understanding how it works behind the scenes)

    <menu:useMenuDisplayer name="CoolMenu4" repository="repository">
   .........
    </menu:useMenuDisplayer>   

Where does it look for the "repository" object? Does it lookup in all contexts (Page, Request, Session, Application)?

Cheers

Tom
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] On Behalf Of Thomas Bednarz
Sent: Friday, June 25, 2004 2:22 AM
To: Struts menu user list
Subject: [struts-menu] Newbee - How to create a dynamic menu in a Struts Action?

Hi,

I usually work with Velocity and not with JSP. However I decided to build my menu with JSP and mix Velocity and JSP. The problem is, that I have no experience with JSP.

To create a dynamic menu I looked at the database sample. I have the following question:

How can I put my menu repository from a Struts Action into a JSP Page Context ?????

I have a Struts Action that builds the menu as follows:

    public ActionForward perform(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        ActionForward af = super.perform(mapping, form, request, response);
        if (af != null)
            return af;
        ........
        buildMenu(session);
        .....
        return (new ActionForward("/Templates/Test.jsp"));
    }
   
    protected void buildMenu(HttpSession session)
    {
        MenuRepository repository = new MenuRepository();
        MenuRepository defaultRepository = (MenuRepository)
        session.getServletContext().getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
        repository.setDisplayers(defaultRepository.getDisplayers());
       
        MenuComponent mc = new MenuComponent();
        mc.setName("Test Menu 1");
        String title = "Menu Title";
        mc.setTitle(title);
        //mc.setLocation("location");
        repository.addMenu(mc);
       
       // WHERE TO PUT THE REPOSITORY OBJECT (IN WHICH CONTEXT??)
       // I should do something like JSPPageContext.setAttribute("reporsitory", repository);
    }

In the Test.jsp I would like do do something like:

    <menu:useMenuDisplayer name="ListMenu" repository="repository">
        <menu:displayMenu name="Test_Menu_1"/>
    </menu:useMenuDisplayer>   

Many thanks for your help!

Tom.


Reply via email to