Results 1 to 5 of 5

Thread: Question with regards to JSP beans

  1. #1
    Join Date
    Apr 2007
    Location
    Victoria, BC., Canada
    Posts
    18

    Thumbs up Question with regards to JSP beans

    Hi,

    There may be something I'm missing, how do you access wired webflow beans from a jsp page? I tried <jsp:useBean ..> thinking that all my beans were singletons and useBean if it can find an existing bean with the same id and scope will use it, but it seems I'm not getting the correct beans.

    Jsp:
    <%@page import=".. bl.Cache ..">
    <jsp:useBean id="blCache" class="bl.Cache" />
    <% String id=blCache.getId() %>

    app-flow-beans.xml:
    <bean id="blCache" class="bl.Cache">
    <property name="logger"><ref bean="logger"/>
    </bean>

    java:
    public class Cache {
    protected ILogger logger;
    protected String id;
    public String getId() { return id; }
    public void setLogger(...)
    }

    Thanks

  2. #2
    Join Date
    Apr 2007
    Location
    Victoria, BC., Canada
    Posts
    18

    Default

    This works:

    In the pre-action (bl-flow.xml):
    <action-state id="doCache">
    <action bean="actionCache"/>
    <transition on="eventStart" to="showWelcome"/>

  3. #3
    Join Date
    Apr 2007
    Location
    Victoria, BC., Canada
    Posts
    18

    Default

    Sorry disregard the previous incomplete message. What I wanted to say was:

    This works:

    In the pre-action (bl-flow.xml):
    <action-state id="doCache">
    <action bean="actionCache"/>
    <transition on="eventStart" to="showWelcome"/>
    ...

    in the ActionCache.java:
    ServletExternalContext externalContext = (ServletExternalContext)context.getExternalContext ();
    HttpServletRequest request = externalContext.getRequest();
    HttpSession session = request.getSession();
    session.setAttribute("blCache", blCache);

    and in the jsp page:
    <%
    bl.Cache blCache =(bl.Cache) request.getSession().getAttribute("blCache");
    %>

    Is that the correct way to do it? Isn't there a simpler way to get to this bean?

    Thanks.

  4. #4
    Join Date
    Apr 2007
    Location
    Victoria, BC., Canada
    Posts
    18

    Default

    I still would like to solicit your opinions on this solution. Is it the right way to go?

    Thanks.

  5. #5
    Join Date
    Sep 2004
    Location
    Leuven, Belgium
    Posts
    1,853

    Default

    The "bean" referenced by an action resides in a Spring application context and is not directly available to a JSP.

    SWF will merge the contents of all its scopes (request, flash, flow, conversation) together and expose that data to the JSP through request attributes. So if you have something like "foo" in your flow scope (or any other scope), you can do in your JSP:

    ${foo}

    <%=request.getAttribute("foo") %>

    <jsp:useBean id="foo" class="..."/>

    Normally you don't want/need to access the 'service' beans difened in your application context from your JSP, so ASAIK there is not easy way to do that.

    Erwin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •