Package org.nutz.lang.util

Examples of org.nutz.lang.util.Context


        }
    }

    public static <T> T evalObj(NutConfig config, Class<T> type, String[] args) {
        // 用上下文替换参数
        Context context = config.getLoadingContext();
        for (int i = 0; i < args.length; i++) {
            args[i] = Segments.replace(args[i], context);
        }
        // 判断是否是 Ioc 注入
View Full Code Here


     * 保存对象到attribute
     */
    public static void putRequestAttribute(HttpServletRequest req, Object re){
        if (null != re){
            if(re instanceof Context){
                Context context = (Context) re;
                for(String key : context.keys()){
                    req.setAttribute(key, context.get(key));
                }
            } else {
                req.setAttribute(ViewProcessor.DEFAULT_ATTRIBUTE, re);
            }
        }
View Full Code Here

    }

    protected void createContext(NutConfig config) {
        // 构建一个上下文对象,方便子类获取更多的环境信息
        // 同时,所有 Filter 和 Adaptor 都可以用 ${app.root} 来填充自己
        Context context = Lang.context();
        String appRoot = config.getAppRoot();
        context.set("app.root", appRoot);

        if (log.isDebugEnabled()) {
            log.debugf(">> app.root = %s", appRoot);
        }

        // 载入环境变量
        for (Entry<String, String> entry : System.getenv().entrySet())
            context.set("env." + entry.getKey(), entry.getValue());
        // 载入系统变量
        for (Entry<Object, Object> entry : System.getProperties().entrySet())
            context.set("sys." + entry.getKey(), entry.getValue());

        if (log.isTraceEnabled()) {
            log.tracef(">>\nCONTEXT %s", Json.toJson(context, JsonFormat.nice()));
        }
        config.getServletContext().setAttribute(Loading.CONTEXT_NAME, context);
View Full Code Here

            if(om.containsKey(right.toString())){
                return om.get(right.toString());
            }
        }
        if (obj instanceof Context) {
            Context sc = (Context) obj;
            if (sc.has(right.toString())) {
                return sc.get(right.toString());
            }
        }
       
        Mirror<?> me = Mirror.me(obj);
        return me.getValue(obj, right.toString());
View Full Code Here

    protected String evalPath(HttpServletRequest req, Object obj) {
        if (null == dest)
            return null;

        Context context = Lang.context();

        // 解析每个表达式
        if (exps.size() != 0) {
          Context expContext = createContext(req, obj);
          for (Entry<String, El> en : exps.entrySet())
            context.set(en.getKey(), en.getValue().eval(expContext));
        }
        // 生成解析后的路径
        return Strings.trim(this.dest.render(context).toString());
View Full Code Here

     *            入口函数的返回值
     * @return 上下文对象
     */
    @SuppressWarnings("unchecked")
    public static Context createContext(HttpServletRequest req, Object obj) {
        Context context = Lang.context();
        // 复制全局的上下文对象
        Object globalContext = Mvcs.getServletContext()
                                   .getAttribute(Loading.CONTEXT_NAME);
        if (globalContext != null) {
            context.putAll((Context) globalContext);
        }

        // 请求对象的属性列表
        Map<String, Object> req_attr = new HashMap<String, Object>();
        for (Enumeration<String> en = req.getAttributeNames(); en.hasMoreElements();) {
            String tem = en.nextElement();
            if (!tem.startsWith("$"))
                req_attr.put(tem, req.getAttribute(tem));
        }
        context.set("a", req_attr);// 兼容最初的写法
        context.set("req_attr", req_attr);

        ActionContext ac = Mvcs.getActionContext();
        if (ac != null)
            context.set("pathargs", Mvcs.getActionContext().getPathArgs());

        HttpSession session = Mvcs.getHttpSession(false);
        if (session != null) {
            Map<String, Object> session_attr = new HashMap<String, Object>();
            for (Enumeration<String> en = session.getAttributeNames(); en.hasMoreElements();) {
                String tem = en.nextElement();
                session_attr.put(tem, session.getAttribute(tem));
            }
            context.set("session_attr", session_attr);
        }
        // 请求的参数表,需要兼容之前的p.参数, Fix issue 418
        Map<String, String> p = new HashMap<String, String>();
        for (Object o : req.getParameterMap().keySet()) {
            String key = (String) o;
            String value = req.getParameter(key);
            p.put(key, value);
            context.set(key, value);// 以支持直接获取请求参数
        }
        context.set("p", p);

        Map<String, String> u = new HashMap<String, String>();
        AtMap at = Mvcs.getAtMap();
        if (at != null) {
            for (Object o : at.keys()) {
                String key = (String) o;
                u.put(key, at.get(key));
            }
            context.set("u", u);
        }

        // 加入返回对象
        if (null != obj)
            context.set(ViewProcessor.DEFAULT_ATTRIBUTE, obj);
        return context;
    }
View Full Code Here

TOP

Related Classes of org.nutz.lang.util.Context

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.