Struts2 18 Interceptor (9)

Source: Internet
Author: User

ScopedModelDrivenInterceptor: the interceptor is in the eighth position of defaultStack. Its main function is to retrieve the corresponding model settings from the specified scope to the Action. This class has three related attributes: scope: where to obtain the model? There are two values: request and session. The default value is requestname: to retrieve the keyclassName: model Class Name of the model in scope, that is, the type of the model. Although these three parameters can be set, struts2 does not assign a value to it in defaultStack. Therefore, the three attributes of the initial value are null. Next we will look at a ScopedModelDriven interface, which inherits from the ModelDriven interface and implements the ScopedModelDriven interface, which also implements ModelDriven. Therefore, the current Action also has the ModelDriven feature. ModelDriven will introduce it in the next interceptor. The ScopedModelDriven interface has four methods: 1. getModel () --> used for Action to get model2.setModel () --> provided to ScopedModelDrivenInterceptor to set model3.getScopeKey () --> used for Action to get the key of the model stored in the scope, this corresponds to the name attribute 4 in ScopedModelDrivenInterceptor. setScopeKey () --> the key provided to ScopedModelDrivenInterceptor to set model storage for Action. below is the source code of the intercept method of ScopedModelDrivenInterceptor: [java] @ Override public String intercept (ActionInvocation invocation) S Exception {Object action = invocation. getAction (); // obtain the currently executed Action // if the Action implements the ScopedModelDriven interface if (action instanceof ScopedModelDriven) {ScopedModelDriven modelDriven = (ScopedModelDriven) action; // convert it to if (modelDriven. getModel () = null) {// If the returned value is null, ActionContext ctx = ActionContext of the model attribute in the Action is not modified if it is not null. getContext (); // get ActionContext ActionConfig config = invocation. getProxy (). GetConfig (); // get Action configuration // model class name. The initial value is null String cName = className; if (cName = null) {try {Method method = action. getClass (). getMethod (GET_MODEL, EMPTY_CLASS_ARRAY); // obtain the getModel method Class cls = method through reflection. getReturnType (); // obtain the return value type of the getModel method. cName = cls. getName (); // get model class name} catch (NoSuchMethodException e) {// If The getModel method is not available, throw new XWorkException ("The" + GET_MODEL + "() is not defin Ed in action "+ action. getClass () + "", config) ;}} String modelName = name; // The key stored in the model. The initial value is null if (modelName = null) {modelName = cName; // assign the Class Name of the model to the key} Object model = resolveModel (objectFactory, ctx, cName, scope, modelName) stored in the model; // parse model modelDriven. setModel (model); // set the returned model object to modelDriven in Action. setScopeKey (modelName); // return the key stored in the model to the Action} return invocation. invoke ();// Calling the next interceptor} the logic of this method is quite simple. If the getModel method of Action returns a value other than null, strust2 will not modify the model attribute of Action, this is more in line with our application requirements, or the interceptor will be too "overbearing", and then parse and obtain the model object, finally, set the returned model Object and the key of the model Object storage to Action. Now let's take a look at the resolveModel method: [java] protected Object resolveModel (ObjectFactory factory, ActionContext actionContext, String modelClassName, string modelScope, String modelName) throws Exception {Object model = null; Map <String, Object> scopeMa P = actionContext. getContextMap (); // get the context Map object if ("session ". equals (modelScope) {// If scope is configured as session, scopeMap = actionContext is not configured here. getSession (); // so it will not be executed here} model = scopeMap. get (modelName); // search for if (model = null) in context Map (that is, the request scope) {// model = factory is not found. buildBean (modelClassName, null); // call the buildBean method of the object factory to instantiate an object scopeMap. put (modelName, model); // place it in context Map} Return model; // return model object} This method first searches for the scope. If it cannot be found, create another ObjectFactory object. ObjectFactory is used by struts2 to create an object, internally, class is called through reflection. newInstance () is created. Action, Result, and Interceptor in sturst2 are created by ObjectFactory. The model object is created and put into the request scope. After the method is executed, the returned model is set to Action. If the Action receives the model, the model in the Action has a value.

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.