views:

561

answers:

2

I would like to render the same JSP for multiple Struts Actions. I'm having trouble because when rendering the JSP, the bean name is different depending on which Action was called. So, I can't call something like:

<c:out value="${myBean.myProperty}" />

because the bean isn't necessarily called myBean.

Currently, I've been getting around this by placing any common objects I'll need in the JSP into the HttpSession. For example:

public class SampleAction extends Action
{
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) throws Exception
    {
        String string = "TEST";
        HttpSession session = request.getSession();
        session.setAttribute("sampleString", string);

        return mapping.findForward("sampleAction");
    }
}

Then, in the JSP, I can simple reference "sampleString" and it won't change depending on the rendering Action.

Of course, the objects I'm using are much larger than a string, and I don't want to put objects in the session if I don't have to. Is there any way to just put an object into the page context or something, rather than the session, in order to share it among different JSPs?

I'm coming from the Rails world, so I'm very new to Struts and am finding myself a bit lost. I have to extend an existing application written in Struts. The program actually uses BeanAction to combine an ActionForm with an Action, so the example above isn't exactly what my code looks like.

Thanks for any help clearing up my confusion!

+1  A: 

I think you want to use an interface that the three different action classes implement, so you can have the same jsp use the interface type. Note that this makes the three action classes "the same" wrt to that jsp, so I'm basically saying that's the route to go. If you cannot/will not use that then I'd probably use a custom jsp tag to encapsulate the common bits and have separate jsp's for each page call the same common tag.

krosenvold
Thanks, yes I definitely would like all of the Action classes to have the same interface to the JSP, so this makes sense.However, what would the bean be called in the JSP? "BeanAction" might be confusing me here. My understanding is that the form that is passed to the action is available in the JSP through the same name as it was defined in struts-config.xml. Is this way off base?If I'm on the right track, then it seems I'd have to have different names for each form, meaning I couldn't use the same JSP for different actions.
MikeQ
+1  A: 

My first thought was to try implementing it as a custom tag, just like krosenvold said in his answer. But you said you are new to Struts, and I will assume you are new to JSP's and J2EE web development too. And custom tag's are not a very easy subject.

You could use a 'proxy' page to call via jsp:include your destination JSP. The jsp:include tag can have a body having jsp:param tags, in which you can map a common variable name to different references.

See here for an example in jsp:param in jsp:include.

By doing that, you will have a single page with you real code, and N proxies to handle referencing to your complex objects. Using custom tags would work too, this is just another way of doing it, which I personally think is easier to implement for a non-SCWCD.

wtaniguchi
I considered this (though I didn't know how to implement it, thanks for the example), and this may be the route I wind up taking. I guess I don't want to make things too complicated for myself since this isn't a huge project.
MikeQ