1

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

你有什么建议吗?

4

3 回答 3

1

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,您需要在 web.xml 中的 Stripes 过滤器中添加一个 init-param:

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

示例动作豆:

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..
  }
}

另一种解决方案,在我看来是一种优越的解决方案,是使用依赖注入框架将(单例)对象注入到您的 actionbean 中。请参阅此处的 Stripes 配置示例:使用 Guice DI 注入 Stripes ActionBeans

于 2011-04-09T20:37:14.110 回答
0

@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 中的对象

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

于 2011-06-05T17:16:16.727 回答
0

不是特定于 Stripes 的方式,而是使用标准的 Servlet API 来实现ServletContextListener并在contextInitialized()方法中完成工作。如果您将其注册为<listener>in web.xml(或者当您已经在 J​​ava EE 6 上,请使用 进行注释@WebListener),那么它将在 webapp 启动期间运行。

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

通过这种方式,它可以在 EL${somename}和所有动作 bean 中通过ServletContext#getAttribute().

于 2011-04-09T15:54:38.857 回答