Struts2 18 Interceptor (10)

Source: Internet
Author: User

ModelDrivenInterceptor: the interceptor is in the ninth position of ultultstack. To make the interceptor valid after ScopedModelDrivenInterceptor, the Action must implement the ModelDriven interface, which is a method: getModel (), the main task of the ModelDrivenInterceptor interceptor is to call the getModel () method of Action and then push the returned model to the value stack (if not null ). If Action implements the ScopedModelDriven interface, it also implements the ModelDriven interface, because ScopedModelDrivenInterceptor will certainly return a model object during execution and then call the setModel (model) method of Action, if the Action receives the model, when the ModelDrivenInterceptor interceptor is executed, the getModel () method of the Action returns the value set by the ScopedModelDrivenInterceptor interceptor, which is no longer null, therefore, the model will naturally be pushed into the value stack. The following is the source code of the intercept method: [java] @ Override public String intercept (ActionInvocation invocation) throws Exception {Object action = invocation. getAction (); // obtain the currently executed Action // if the Action implements the ModelDriven interface if (action instanceof ModelDriven) {ModelDriven modelDriven = (ModelDriven) action; ValueStack stack = invocation. getStack (); Object model = modelDriven. getModel (); // obtain the model if (model! = Null) {// if the model is not null, the model is pushed to the value stack. push (model) ;}if (refreshModelBeforeResult) {// whether to update the model object before the Result is executed. The default value is false invocation. addPreResultListener (new RefreshModelBeforeResult (modelDriven, model) ;}} return invocation. invoke (); // call the next interceptor} The method logic is very simple. As mentioned above, it is to press the result returned by the getModel method into the value stack, we generally implement this interface by using the model object pushed into the value stack to receive data submitted from the page. In many cases, we write attributes in the Action to receive parameters, because Action is also in the value stack, struts2 searches The object of the corresponding setter method, and the model is pushed into the value stack, which is at the top of the stack, so the parameters submitted from the page are also received by the model object. In this way, the model object is a bit like the formbean object function in struts1.X. The ModelDrivenInterceptor interceptor also has a property named refreshModelBeforeResult, which is used to set whether to update the model object before the Result is executed. If the default value is false, the model object will not be updated, we may point the referenced model variable to another object during the Action execution. If you set refreshModelBeforeResult to true, the PreResultListener will replace the objects in the value stack with the new object before the Result is executed to achieve the update effect. PreResultListener is a listener registered by the ActionInvocation object. Before the Result is executed, the beforeResult method of the listener is executed. Although in defaultStack, struts2 does not register this RefreshModelBeforeResult listener in ActionInvocation, let's take a simple look: [java] protected static class implements PreResultListener {private Object originalModel = null; protected ModelDriven action; public RefreshModelBeforeResult (ModelDriven action, Object model) {this. originalModel = model; this. action = action;} public void beforeResult (ActionInvocati On invocation, String resultCode) {ValueStack stack = invocation. getStack (); // get the value stack CompoundRoot root = stack. getRoot (); // obtain the value Stack's root Object boolean needsRefresh = true; Object newModel = action. getModel (); // obtain the new model Object from the Action // Check to see if the new model instance is already on the stack for (Object item: root) {if (item. equals (newModel) {// if the new model object is the same as the old one, needsRefresh = false;} // Add th E new model on the stack if (needsRefresh) {// if you want to refresh // Clear off the old model instance if (originalModel! = Null) {root. remove (originalModel); // first remove the old model} if (newModel! = Null) {stack. push (newModel); // press the new model object into the value stack }}} RefreshModelBeforeResult to implement the PreResultListener interface, which is a static internal class of ModelDrivenInterceptor, it is easy to understand how to refresh the beforeResult method before executing the Result.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.