Search in sources :

Example 1 with Before

use of org.eweb4j.mvc.interceptor.Before in project eweb4j-framework by laiweiwei.

the class ActionExecution method execute.

/**
	 * 执行Action
	 * 
	 * @throws Exception
	 */
public void execute() throws Exception {
    // 实例化pojo
    initPojo();
    // IOC注入对象到pojo中
    injectIocBean();
    String methodName = this.context.getActionConfigBean().getMethod();
    Method[] methods = ru.getMethods(methodName);
    if (methods == null || methods.length == 0)
        return;
    method = this.getFirstMethd(methods);
    if (method == null)
        return;
    //		Consumes cons = method.getAnnotation(Consumes.class);
    boolean hasConsumes = JAXWSUtil.hasConsumes(method);
    if (hasConsumes) {
        String[] cts = ConsumesUtil.getConsumesValue(method);
        for (String ct : cts) {
            if ("json".equals(ct) || MIMEType.JSON.equals(ct)) {
                String[] jss = this.context.getQueryParamMap().get("_json_data_");
                if (jss != null) {
                    for (String json : jss) {
                        Map<String, Object> map;
                        try {
                            map = CommonUtil.parse(json, Map.class);
                        } catch (Exception e) {
                            e.printStackTrace();
                            break;
                        }
                        for (Iterator<Entry<String, Object>> it = map.entrySet().iterator(); it.hasNext(); ) {
                            Entry<String, Object> e = it.next();
                            String key = e.getKey();
                            String val = String.valueOf(e.getValue());
                            //继续解析,如果解析成功说明是多级的json格式,最后将其平展开来
                            try {
                                Map<String, Object> subMap = CommonUtil.parse(val, Map.class);
                                for (Iterator<Entry<String, Object>> subIt = subMap.entrySet().iterator(); subIt.hasNext(); ) {
                                    Entry<String, Object> subE = subIt.next();
                                    String subKey = subE.getKey();
                                    String subVal = String.valueOf(subE.getValue());
                                    String newKey = key + "." + subKey;
                                    try {
                                        Map<String, Object> subSubMap = CommonUtil.parse(subVal, Map.class);
                                        for (Iterator<Entry<String, Object>> subSubIt = subSubMap.entrySet().iterator(); subSubIt.hasNext(); ) {
                                            Entry<String, Object> subSubE = subSubIt.next();
                                            String subSubKey = subSubE.getKey();
                                            String subSubVal = String.valueOf(subSubE.getValue());
                                            String newSubKey = newKey + "." + subSubKey;
                                            assemJsonData(newSubKey, subSubVal);
                                        }
                                    } catch (Exception ex2) {
                                        assemJsonData(newKey, subVal);
                                    }
                                }
                            } catch (Exception ex) {
                                assemJsonData(key, val);
                            }
                        }
                    }
                }
            }
        }
    }
    // 注入框架mvc action 上下文环境
    this.injectActionCxt2Pojo(this.ru);
    //		if (IAction.class.isAssignableFrom(this.actionObject.getClass())) {
    //			// struts2风格
    //			IAction action = (IAction) actionObject;
    //			action.init(this.context);
    //			retn = action.execute();
    //			// 对Action执行返回结果的处理
    //			this.handleResult();
    //			
    //			return ;
    //		}
    // 执行验证器
    this.handleValidator();
    try {
        // 注入mvc action 请求参数
        ParamUtil.injectParam(this.context, this.ru, null);
        /* 方法体内的前置拦截器执行  */
        Before before = method.getAnnotation(Before.class);
        if (before != null) {
            InterExecution before_interExe = new InterExecution("before", context);
            before_interExe.execute(before.value());
            if (before_interExe.getError() != null) {
                before_interExe.showErr();
                return;
            }
        }
        // execute the action method
        excuteMethod(methodName);
        /* 方法体内的后置拦截器执行  */
        After after = method.getAnnotation(After.class);
        if (after != null) {
            // 后置拦截器
            InterExecution after_interExe = new InterExecution("after", context);
            after_interExe.execute(after.value());
            if (after_interExe.getError() != null) {
                after_interExe.showErr();
                return;
            }
        }
        /* 外部配置的后置拦截器后执行 */
        // 7.后置拦截器
        InterExecution after_interExe = new InterExecution("after", this.context);
        if (after_interExe.findAndExecuteInter()) {
            after_interExe.showErr();
            return;
        }
        // 对Action执行返回结果的处理
        this.handleResult();
    } catch (Exception e) {
        throw e;
    }
}
Also used : Before(org.eweb4j.mvc.interceptor.Before) Method(java.lang.reflect.Method) IOException(java.io.IOException) ZipEntry(java.util.zip.ZipEntry) Entry(java.util.Map.Entry) InterExecution(org.eweb4j.mvc.interceptor.InterExecution) After(org.eweb4j.mvc.interceptor.After) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ZipEntry (java.util.zip.ZipEntry)1 After (org.eweb4j.mvc.interceptor.After)1 Before (org.eweb4j.mvc.interceptor.Before)1 InterExecution (org.eweb4j.mvc.interceptor.InterExecution)1