Search in sources :

Example 1 with ViewMaker

use of org.nutz.mvc.ViewMaker in project nutz by nutzam.

the class ViewZone method makeView.

public static View makeView(NutConfig config, ActionInfo ai, String viewType, boolean allowProxy) {
    if (Strings.isBlank(viewType))
        return new VoidView();
    String str = viewType;
    int pos = str.indexOf(':');
    String type, value;
    if (pos > 0) {
        type = Strings.trim(str.substring(0, pos).toLowerCase());
        value = Strings.trim(pos >= (str.length() - 1) ? null : str.substring(pos + 1));
    } else {
        type = str;
        value = null;
    }
    if (allowProxy && "re".equals(type)) {
        View dft = null;
        if (value != null)
            dft = makeView(config, ai, value, false);
        return new ViewZone(config, ai, dft);
    }
    for (ViewMaker maker : ai.getViewMakers()) {
        if (maker instanceof ViewMaker2) {
            View view = ((ViewMaker2) maker).make(config, ai, type, value);
            if (view != null)
                return view;
        }
        View view = maker.make(config.getIoc(), type, value);
        if (null != view)
            return view;
    }
    throw Lang.makeThrow("Can not eval %s(\"%s\") View for %s", viewType, str, ai.getMethod());
}
Also used : ViewMaker(org.nutz.mvc.ViewMaker) ViewMaker2(org.nutz.mvc.ViewMaker2) View(org.nutz.mvc.View)

Example 2 with ViewMaker

use of org.nutz.mvc.ViewMaker in project nutz by nutzam.

the class NutLoading method createViewMakers.

protected ViewMaker[] createViewMakers(Class<?> mainModule, Ioc ioc) throws Exception {
    Views vms = mainModule.getAnnotation(Views.class);
    List<ViewMaker> makers = new ArrayList<ViewMaker>();
    if (null != vms) {
        for (int i = 0; i < vms.value().length; i++) {
            if (vms.value()[i].getAnnotation(IocBean.class) != null && ioc != null) {
                makers.add(ioc.get(vms.value()[i]));
            } else {
                makers.add(Mirror.me(vms.value()[i]).born());
            }
        }
    } else {
        if (ioc != null) {
            String[] names = ioc.getNames();
            Arrays.sort(names);
            for (String name : ioc.getNames()) {
                if (name != null && name.startsWith(ViewMaker.IOCNAME)) {
                    log.debug("add ViewMaker from Ioc by name=" + name);
                    makers.add(ioc.get(ViewMaker.class, name));
                }
            }
        }
    }
    // 优先使用用户自定义
    makers.add(new DefaultViewMaker());
    if (log.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        for (ViewMaker maker : makers) {
            sb.append(maker.getClass().getSimpleName()).append(".class,");
        }
        sb.setLength(sb.length() - 1);
        log.debugf("@Views(%s)", sb);
    }
    return makers.toArray(new ViewMaker[makers.size()]);
}
Also used : Views(org.nutz.mvc.annotation.Views) DefaultViewMaker(org.nutz.mvc.view.DefaultViewMaker) ViewMaker(org.nutz.mvc.ViewMaker) ArrayList(java.util.ArrayList) DefaultViewMaker(org.nutz.mvc.view.DefaultViewMaker)

Example 3 with ViewMaker

use of org.nutz.mvc.ViewMaker in project nutz by nutzam.

the class NutLoading method evalUrlMapping.

protected UrlMapping evalUrlMapping(NutConfig config, Class<?> mainModule, Ioc ioc) throws Exception {
    /*
         * @ TODO 个人建议可以将这个方法所涉及的内容转换到Loadings类或相应的组装类中,
         * 以便将本类加以隔离,使本的职责仅限于MVC整体的初使化,而不再负责UrlMapping的加载
         */
    /*
         * 准备 UrlMapping
         */
    UrlMapping mapping = createUrlMapping(config);
    if (log.isInfoEnabled())
        log.infof("Build URL mapping by %s ...", mapping.getClass().getName());
    /*
         * 创建视图工厂
         */
    ViewMaker[] makers = createViewMakers(mainModule, ioc);
    /*
         * 创建动作链工厂
         */
    ActionChainMaker maker = createChainMaker(config, mainModule);
    /*
         * 创建主模块的配置信息
         */
    ActionInfo mainInfo = Loadings.createInfo(mainModule);
    /*
         * 准备要加载的模块列表
         */
    // TODO 为什么用Set呢? 用List不是更快吗?
    Set<Class<?>> modules = Loadings.scanModules(ioc, mainModule);
    if (modules.isEmpty()) {
        if (log.isWarnEnabled())
            log.warn("None module classes found!!!");
    }
    int atMethods = 0;
    /*
         * 分析所有的子模块
         */
    for (Class<?> module : modules) {
        ActionInfo moduleInfo = Loadings.createInfo(module).mergeWith(mainInfo);
        for (Method method : module.getMethods()) {
            if (!Modifier.isPublic(method.getModifiers()) || method.isBridge())
                continue;
            if (Mirror.getAnnotationDeep(method, At.class) == null)
                continue;
            // 增加到映射中
            ActionInfo info = Loadings.createInfo(method).mergeWith(moduleInfo);
            info.setViewMakers(makers);
            mapping.add(maker, info, config);
            atMethods++;
        }
        // 记录pathMap
        if (null != moduleInfo.getPathMap()) {
            for (Entry<String, String> en : moduleInfo.getPathMap().entrySet()) {
                config.getAtMap().add(en.getKey(), en.getValue());
            }
        }
    }
    if (atMethods == 0) {
        if (log.isWarnEnabled())
            log.warn("None @At found in any modules class!!");
    } else {
        log.infof("Found %d module methods", atMethods);
    }
    config.setUrlMapping(mapping);
    config.setActionChainMaker(maker);
    config.setViewMakers(makers);
    return mapping;
}
Also used : UrlMapping(org.nutz.mvc.UrlMapping) ActionChainMaker(org.nutz.mvc.ActionChainMaker) ActionInfo(org.nutz.mvc.ActionInfo) Method(java.lang.reflect.Method) At(org.nutz.mvc.annotation.At) DefaultViewMaker(org.nutz.mvc.view.DefaultViewMaker) ViewMaker(org.nutz.mvc.ViewMaker)

Aggregations

ViewMaker (org.nutz.mvc.ViewMaker)3 DefaultViewMaker (org.nutz.mvc.view.DefaultViewMaker)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ActionChainMaker (org.nutz.mvc.ActionChainMaker)1 ActionInfo (org.nutz.mvc.ActionInfo)1 UrlMapping (org.nutz.mvc.UrlMapping)1 View (org.nutz.mvc.View)1 ViewMaker2 (org.nutz.mvc.ViewMaker2)1 At (org.nutz.mvc.annotation.At)1 Views (org.nutz.mvc.annotation.Views)1