Stripes 中的实例变量

发布于 2024-10-31 11:27:14 字数 302 浏览 2 评论 0原文

我正在尝试找到一种在 Stripes 应用程序上下文中创建实例变量的方法。 在使用手工编码的 Servlet 时,我会在 Servlet 的 init() 方法中执行一些操作。 问题在于,由于每次访问应用程序时都会创建 ActionBean 的实例,因此会多次创建 actionBean 中的变量。 我试图通过 Stripes 尝试通过 ActionBeanContext.getServletContext() 调用 ServletContext 找到一些合理的位置,但从那里无法访问 init () 方法并在其中编写一些代码。

您有什么建议吗?

I'm trying to find a way to create an instance variable within the Stripes application context.
Something that i would do in the init() method of a Servlet while using hand-coded servlets.
The problem is that since an instance of the ActionBean is created each time the application is accessed, the variable in the actionBean is created multiple time.
I have tried to get some reasonable place withing Stripes trying to call the ServletContext via ActionBeanContext.getServletContext(), but from there there is no way to access the init() method and write some code in it.

Do you have any suggestions?

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

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

发布评论

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

评论(3

如此安好 2024-11-07 11:27:14

ActionBeanContext 也是 Stripes 应用程序上下文。该上下文可以自定义,并且可以包含您想要的任何内容。一些示例代码:

package my.app;

public class CustomActionBeanContext extends ActionBeanContext {
  public CustomActionBeanContext() {
    super();
  }

  public MyObject getMyObject() {
      return (MyObject) getServletContext().getAttribute(“myObject”);
  }

  // Alternative solution without ServletContextListner
  private static MyObject2 myObject2;
  static {
     myObject2 = new MyObject2();
  }

  public MyObject2 getMyObject2() {
      return myObject2;
  }
}

Stripes 上下文工厂 知道您想要的要使用自定义 ActionBeanContext,您需要将 init-param 添加到 web.xml 中的 Stripes 过滤器:

    <init-param>
        <param-name>ActionBeanContext.Class</param-name>
        <param-value>my.app.CustomActionBeanContext</param-value>
    </init-param>

您可以通过添加 SerlvetContextListener 在服务器启动时初始化对象:

Public class MyServletContextListener implements ServletContextListener {
@Override
  public void contextInitialized(ServletContextEvent event) {
    event.getServletContext().setAttribute("myObject", new MyObject());
}

ActionBean 示例:

public class MyAction implements ActionBean {
  private CustomActionBeanContext context;

  @Override
  public CustomActionBeanContext getContext() {
    return context;
  }

  @Override
  public void setContext(ActionBeanContext context) {
    this.context = (CustomActionBeanContext) context;
  }

  @DefaultHandler
  public Resolution view() {
    MyObject  myObject = getContext().getMyObject();
    // doing something usefull with it..
  }
}

另一种解决方案,在我看来更好的解决方案是使用 依赖注入 框架来注入 (singleton) 对象到你的actionbeans中。请参阅此处的 Stripes 配置示例:使用 Guice DI 注入 Stripes ActionBeans

The ActionBeanContext is also Stripes application context. This context can be customized and can contain whatever you want. Some example code:

package my.app;

public class CustomActionBeanContext extends ActionBeanContext {
  public CustomActionBeanContext() {
    super();
  }

  public MyObject getMyObject() {
      return (MyObject) getServletContext().getAttribute(“myObject”);
  }

  // Alternative solution without ServletContextListner
  private static MyObject2 myObject2;
  static {
     myObject2 = new MyObject2();
  }

  public MyObject2 getMyObject2() {
      return myObject2;
  }
}

To let the Stripes context factory know you want to use a custom ActionBeanContext you need to add an init-param to the Stripes filter in the web.xml:

    <init-param>
        <param-name>ActionBeanContext.Class</param-name>
        <param-value>my.app.CustomActionBeanContext</param-value>
    </init-param>

You can initialize your object at server start by adding a SerlvetContextListener:

Public class MyServletContextListener implements ServletContextListener {
@Override
  public void contextInitialized(ServletContextEvent event) {
    event.getServletContext().setAttribute("myObject", new MyObject());
}

Example ActionBean:

public class MyAction implements ActionBean {
  private CustomActionBeanContext context;

  @Override
  public CustomActionBeanContext getContext() {
    return context;
  }

  @Override
  public void setContext(ActionBeanContext context) {
    this.context = (CustomActionBeanContext) context;
  }

  @DefaultHandler
  public Resolution view() {
    MyObject  myObject = getContext().getMyObject();
    // doing something usefull with it..
  }
}

An alternative solution, in my opinion a superiour solution, is to use a dependency injection framework for injecting the (singleton) objects into your actionbeans. See Stripes configuration example here: Injecting Stripes ActionBeans with Guice DI

花之痕靓丽 2024-11-07 11:27:14

不是 Stripes 特定的方式,而是使用标准 Servlet API,您可以实现 ServletContextListener 并在 contextInitialized() 方法中完成这项工作。如果您在 web.xml 中将其注册为 (或者当您已经使用 Java EE 6 时,请使用 @WebListener 进行注释) ),然后它将在 webapp 启动期间运行。

@Override
public void contextInitialized(ServletContextEvent event) {
    event.getServletContext().setAttribute("somename", new SomeObject());
}

这样,它可以通过 ${somename} 在 EL 中使用,并通过 ServletContext#getAttribute() 在所有操作 bean 中使用。

Not a Stripes-specific way, but using the standard Servlet API you'd implement ServletContextListener and do the job in contextInitialized() method. If you register it as <listener> in web.xml (or when you're already on Java EE 6, annotate using @WebListener), then it'll run during webapp's startup.

@Override
public void contextInitialized(ServletContextEvent event) {
    event.getServletContext().setAttribute("somename", new SomeObject());
}

This way it's available in EL by ${somename} and in all action beans by ServletContext#getAttribute().

ゝ杯具 2024-11-07 11:27:14

@JBoy,您必须在 web.xml 中指定 ServletContextListner 的实现,如下所示,

<listner>
   <listner-class>
        www.test.com.MyListner
   </listner-class>
</listner>

感谢 KDeveloper 的建议。我也在寻找解决方案。我从他的博客上找到了信息

我发现了另一种方法。为此,您必须对“RuntimeConfiguration”类进行子类化

public class MyConfiguration extends RuntimeConfiguration {
     @Override
     public void init() {
         getServletContext.setAttribute("myObject",new MyObject);
         super.init();
     }
}

,然后在 web.xml 中指定上述配置。

<init-param>
   <param-name>Configuration.Class</param-name>
   <param-value>www.test.com.MyConfiguration</param-value>
</init-param>

正如 KDeveloper 所说,您还必须对 ActionBeanContext 进行子类化;获取 ActionBeans 中的对象

这是我的发现。我发现它正在发挥作用。但不知道有没有副作用。如果有的话;请评论..

@JBoy, You have to specify your implementation of ServletContextListner in the web.xml like below

<listner>
   <listner-class>
        www.test.com.MyListner
   </listner-class>
</listner>

Thanks KDeveloper for his advice. I was also searching for the solution. I found the information from his blog

There is one more method I have found out. For that you have to subclass the "RuntimeConfiguration" class

public class MyConfiguration extends RuntimeConfiguration {
     @Override
     public void init() {
         getServletContext.setAttribute("myObject",new MyObject);
         super.init();
     }
}

After that in the web.xml specify the above configuration.

<init-param>
   <param-name>Configuration.Class</param-name>
   <param-value>www.test.com.MyConfiguration</param-value>
</init-param>

You also have to subclass the ActionBeanContext as KDeveloper said; to get the object in ActionBeans

This is my finding. I found out it is working. But I don't know whether it has any side effects. If it has any; please comment..

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