पूरी तरह से http://blog.agilelogicsolutions.com/2011/05/struts-2-interceptors-before-between.html में वर्णित के बाद निष्पादित करेंगे के बाद लिखेंगे के रूप में कार्य करता है
आप इंटरसेप्टर हो सकते हैं:
- कार्रवाई
पहले कार्रवाई के बीच
- और परिणाम
- के बाद दृश्य गाया
के रूप में साइट यहाँ में वर्णित कोड नमूने हैं
इंटरसेप्टर
पहले
public class BeforeInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// do something before invoke
doSomeLogic();
// invocation continue
return invocation.invoke();
}
}
}
कार्रवाई और परिणाम के बीच
public class BetweenActionAndResultInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// Register a PreResultListener and implement the beforeReslut method
invocation.addPreResultListener(new PreResultListener() {
@Override
public void beforeResult(ActionInvocation invocation, String resultCode) {
Object o = invocation.getAction();
try{
if(o instanceof MyAction){
((MyAction) o).someLogicAfterActionBeforeView();
}
//or someLogicBeforeView()
}catch(Exception e){
invocation.setResultCode("error");
}
}
});
// Invocation Continue
return invocation.invoke();
}
}
}
गाया के बाद देखें
public class AfterViewRenderedInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// do something before invoke
try{
// invocation continue
return invocation.invoke();
}catch(Exception e){
// You cannot change the result code here though, such as:
// return "error";
// This line will not work because view is already generated
doSomeLogicAfterView();
}
}
}
}
स्रोत
2015-06-08 07:11:30
मैं समझता हूँ कि 'अवरोधन()' पूर्व प्रसंस्करण कहा जाता है, लेकिन क्या विधि पोस्ट प्रोसेसिंग कहा जाता है? क्या हर अनुरोध के लिए हमेशा नष्ट हो रहा है, या सिर्फ एक बार में नष्ट हो रहा है? –
देखें कि मैंने अपना उत्तर – PSR
बहुत बढ़िया अपडेट किया है, बहुत बहुत धन्यवाद। जैसे ही यह मुझे देता है मैं आपके उत्तर को स्वीकार करूंगा। एक और सवाल, क्या होगा यदि मैं कार्रवाई की एक कस्टम विधि को चलाने के लिए चाहता था, उदाहरण के लिए यदि मैं 'invAction.setSomeValue()' को 'invocation.invoke()' से पहले चलाने के लिए चाहता हूं, तो क्या आप जानते हैं कि यह कैसे किया जा सकता है? –