ScopedModelDrivenInterceptorÀ¹½ØÆ÷¼ò½é
¸ÃÀ¹½ØÆ÷´¦ÓÚdefaultStackµÚ°ËµÄλÖã¬ÆäÖ÷Òª¹¦ÄÜÊÇ´ÓÖ¸¶¨µÄ×÷ÓÃÓòÄÚ¼ìË÷ÏàÓ¦µÄmodelÉèÖõ½ActionÖУ¬¸ÃÀàÖÐÓÐÈý¸öÏà¹ØµÄÊôÐÔ£º
scope:´ÓÄÄÀï»ñÈ¡model£¬ÓÐÁ½¸öÖµ£¬Ò»ÊÇrequest,¶þÊÇsession£¬Ä¬ÈÏֵΪrequest
name:ÔÚscopeÖмìË÷modelµÄkey
className:modelÀàµÄÃû³Æ£¬Ò²¾ÍÊǸÃmodelÊÇʲôÀàÐÍ
ËäÈ»ÕâÈý¸ö²ÎÊý¶¼¿ÉÒÔ½øÐÐÉèÖ㬵«ÊÇÔÚdefaultStackÖÐstruts2²¢Ã»ÓÐΪÆ丳ֵ£¬ËùÒÔ³õʼֵÕâÈý¸öÊôÐÔ¶¼Îªnull¡£
ÏÂÃæÎÒÃÇÈ¥¿´Ò»¸öScopedModelDriven½Ó¿Ú£¬¸Ã½Ó¿Ú¼Ì³Ð×ÔModelDriven½Ó¿Ú£¬ÊµÏÖÁËScopedModelDriven½Ó¿ÚÒ²¾ÍʵÏÖÁËModelDriven£¬ËùÒÔµ±Ç°ActionÒ²»áÓÐModelDrivenµÄÌØÐÔ£¬ModelDrivenÔÚÏÂÒ»¸öÀ¹½ØÆ÷ÖнéÉÜ¡£ScopedModelDriven½Ó¿ÚÖÐÏ൱ÓÚÓÐËĸö·½·¨£º
1. getModel() -->ÓÃÓÚAction»ñÈ¡model
2. setModel() -->Ìṩ¸øScopedModelDrivenInterceptorΪActionÉèÖÃmodel
3. getScopeKey() -->ÓÃÓÚAction»ñÈ¡model´æ´¢ÔÚ×÷ÓÃÓòÖеÄkey£¬ÕâÀï¶ÔÓ¦ÁËScopedModelDrivenInterceptorÖеÄnameÊôÐÔ
4. setScopeKey() -->Ìṩ¸øScopedModelDrivenInterceptorΪActionÉèÖÃmodel´æ´¢µÄkey
ÏÂÃæÊÇScopedModelDrivenInterceptorµÄintercept·½·¨Ô´Â룺
±í1
@Override public String intercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction();//»ñÈ¡µ±Ç°ÕýÔÚÖ´ÐеÄAction //Èç¹ûActionʵÏÖÁËScopedModelDriven½Ó¿Ú if (action instanceof ScopedModelDriven) { ScopedModelDriven modelDriven = (ScopedModelDriven) action;//ǿת if (modelDriven.getModel() == null) {//Èç¹û·µ»ØΪnull£¬Èç¹û²»ÎªnullµÄ»°ÊDz»»áÈ¥Ð޸ĸÃActionÖÐmodelÊôÐ﵀ ActionContext ctx = ActionContext.getContext();//»ñÈ¡ActionContext ActionConfig config = invocation.getProxy().getConfig();//»ñÈ¡ActionÅäÖà //modelÀàÃû£¬³õʼֵΪnull String cName = className; if (cName == null) { try { Method method = action.getClass().getMethod(GET_MODEL, EMPTY_CLASS_ARRAY);//ͨ¹ý·´Éä»ñÈ¡getModel·½·¨ Class cls = method.getReturnType();//»ñÈ¡getModel·½·¨µÄ·µ»ØÖµÀàÐÍ cName = cls.getName();//»ñÈ¡modelÀàÃû } catch (NoSuchMethodException e) {//Èç¹ûûÓÐgetModel·½·¨ÔòÅ×Òì³£ thrownew XWorkException("The " + GET_MODEL + "() is not defined in action " + action.getClass() + "", config); } } String modelName = name;//model´æ´¢µÄkey£¬³õʼֵΪnull if (modelName == null) { modelName = cName;//°ÑmodelµÄÀàÃû¸³¸ømodel´æ´¢µÄkey } Object model = resolveModel(objectFactory, ctx, cName, scope, modelName);//½âÎömodel modelDriven.setModel(model);//°Ñ·µ»ØµÄmodel¶ÔÏóÉèÖõ½ActionÖÐ modelDriven.setScopeKey(modelName);//·µ»Ømodel´æ´¢µÄkeyÉèÖõ½ActionÖÐ } } return invocation.invoke();//µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷ } |
¸Ã·½·¨µÄÂß¼»¹ÊÇͦ¼òµ¥µÄ£¬Èç¹ûActionµÄgetModel·½·¨·µ»Ø²»ÎªnullµÄ»°£¬strust2¾Í²»»áÈ¥ÐÞ¸ÄActionµÄmodelÊôÐÔ£¬ÕâÒ²¸ü·ûºÏÎÒÃǵÄÓ¦ÓÃ
ÒªÇ󣬲»È»Õâ¸öÀ¹½ØÆ÷¾ÍÌ«"°ÔµÀ"ÁË£¬È»ºóÈ¥½âÎö»ñÈ¡model¶ÔÏó£¬×îºó°Ñ·µ»ØµÄmodel¶ÔÏóÓëmodel¶ÔÏó´æ´¢µÄkeyÉèÖõ½ActionÖУ¬ÏÖÔÚÈ¥¿´Ò»ÏÂ
resolveModel·½·¨£º
±í2
protected Object resolveModel(ObjectFactory factory, ActionContext actionContext, String modelClassName, String modelScope, String modelName) throws Exception { Object model = null; //»ñÈ¡ActionContextÄÚÈݵÄcontext Map¶ÔÏó Map<String, Object> scopeMap = actionContext.getContextMap();
//Èç¹ûÅäÖÃÁËscopeΪsession£¬µ«ÕâÀﲢûÓÐÅäÖà if ("session".equals(modelScope)) { scopeMap = actionContext.getSession();//ËùÒÔÕâÀï²»»áÖ´ÐÐ }
//È¥context Map(¼´Ï൱ÓÚrequest×÷ÓÃÓò)ÖвéÕÒ model = scopeMap.get(modelName); if (model == null) {//ûÓÐÕÒµ½ //µ÷ÓöÔÏ󹤳§µÄbuildBean·½·¨ÊµÀý»¯Ò»¸ö¶ÔÏó model = factory.buildBean(modelClassName, null);
scopeMap.put(modelName, model);//·ÅÖõ½context MapÖÐ } return model;//·µ»Ømodel¶ÔÏó } |
¸Ã·½·¨ÏÈÈ¥×÷ÓÃÓò²éÕÒ£¬Èç¹û²éÕÒ²»µ½ÔÙÓÉObjectFactory¶ÔÏó´´½¨Ò»¸ö,ObjectFactoryÊÇstruts2ÖÐרÃÅÓÃÓÚ´´½¨¶ÔÏóµÄ£¬ÆäÄÚ²¿¾ÍÊÇͨ¹ý·´Éäµ÷ÓÃclass.newInstance()´´½¨£¬sturst2ÖеÄAction¡¢Result¡¢Interceptor¶¼ÊÇÓÉObjectFactory´´½¨¡£model¶ÔÏó´´½¨ºÃºó·ÅÈëµ½ÁËrequest×÷ÓÃÓòÖУ¬¸Ã·½·¨Ö´ÐÐÍê³ÉºóÓÖ°Ñ·µ»ØµÄmodelÉèÖýøÁËAction£¬Èç¹ûAction¶Ômodel½øÐÐÁ˽ÓÊÕ£¬ÄÇôActionÖеÄmodelÊÇÓÐÖµµÄ¡£
µ½Õâ¸ÃÀ¹½ØÆ÷¾Í½²½âÍê±ÏÁË£¬×îºóµ÷ÓÃinvocation.invoke();µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷