在不同的 Struts 操作类之间有效地共享 JSP

发布于 2024-07-27 13:52:10 字数 1032 浏览 6 评论 0原文

我想为多个 Struts 操作呈现相同的 JSP。 我遇到了麻烦,因为在渲染 JSP 时,bean 名称根据调用的 Action 不同而不同。 因此,我不能调用类似以下内容的内容:

因为 bean 不一定称为 myBean。

目前,我一直在通过将 JSP 中需要的任何常见对象放入 HttpSession 来解决这个问题。 例如:

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");
    }
}

然后,在JSP中,我可以简单地引用“sampleString”,它不会根据渲染Action而改变。

当然,我使用的对象比字符串大得多,如果不需要,我不想将对象放入会话中。 有没有什么方法可以将对象放入页面上下文或其他内容中,而不是会话中,以便在不同的 JSP 之间共享它?

我来自 Rails 世界,所以对 Struts 还很陌生,并且发现自己有点迷失。 我必须扩展用 Struts 编写的现有应用程序。 该程序实际上使用 BeanAction 将 ActionForm 与 A​​ction 组合在一起,因此上面的示例并不完全是我的代码的样子。

感谢您帮助消除我的困惑!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2024-08-03 13:52:10

我的第一个想法是尝试将其实现为自定义标签,就像 krosenvold 在他的回答中所说的那样。 但您说您是 Struts 新手,我假设您对 JSP 和 J2EE Web 开发也是新手。 自定义标签并不是一个很容易的主题。

您可以使用“代理”页面通过 jsp:include 您的目标 JSP 进行调用。 jsp:include 标记可以有一个包含 jsp:param 标记的主体,您可以在其中将公共变量名称映射到不同的引用。

请参见此处 jsp:include 中 jsp:param 的示例

通过这样做,您将拥有一个包含真实代码的页面,以及 N 个代理来处理对复杂对象的引用。
使用自定义标签也可以,这只是另一种方法,我个人认为对于非 SCWCD 来说更容易实现。

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.

放血 2024-08-03 13:52:10

我认为您想要使用三个不同操作类实现的接口,这样您就可以让相同的 jsp 使用该接口类型。 请注意,这使得三个操作类对于该 jsp 来说“相同”,所以我基本上是说这就是要走的路线。 如果您不能/不会使用它,那么我可能会使用自定义 jsp 标记来封装公共位,并为每个页面调用相同的公共标记提供单独的 jsp。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文