ActionBeanContext도 줄무늬 응용 프로그램 컨텍스트입니다. 이 컨텍스트는 사용자 정의 할 수 있으며 원하는 내용을 포함 할 수 있습니다. 일부 예제 코드 :
<init-param>
<param-name>ActionBeanContext.Class</param-name>
<param-value>my.app.CustomActionBeanContext</param-value>
</init-param>
당신은 할 수 있습니다 :
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 context factory은 당신이 web.xml의 줄무늬 필터는 init-PARAM를 추가 할 필요가 ActionBeanContext 사용자 지정을 사용하려면 알려
Public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
event.getServletContext().setAttribute("myObject", new MyObject());
}
예하는 ActionBean :
SerlvetContextListener을 추가하여 서버 시작시 개체를 초기화
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..
}
}
대체 솔루션, 내 의견 superiour 솔루션에서 (singleton가) 당신의 actionbeans에 객체를 주입하기위한 dependency injection 프레임 워크를 사용하는 것입니다. 줄무늬 구성 여기 예를 참조하십시오 : Injecting Stripes ActionBeans with Guice DI
그냥 응용 프로그램 다양한 변수를 만드는 방법을 묻지 않니? (서블릿의'init()'메소드에서 보통 그렇게한다). 인스턴스 변수와는 꽤 다릅니다. – BalusC
안녕하세요 Balus, 실제로, 당신은 줄무늬에 응용 프로그램 넓은 변수를 배치합니까, 난 아직도 찾고있다 – JBoy