Search in sources :

Example 1 with ActionContext

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

the class AbstractPathView method createContext.

/**
     * 为一次 HTTP 请求,创建一个可以被表达式引擎接受的上下文对象
     * 
     * @param req
     *            HTTP 请求对象
     * @param obj
     *            入口函数的返回值
     * @return 上下文对象
     */
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 : Lang.enum2collection(req.getParameterNames(), new ArrayList<String>())) {
        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;
}
Also used : ActionContext(org.nutz.mvc.ActionContext) Context(org.nutz.lang.util.Context) AtMap(org.nutz.mvc.config.AtMap) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) ActionContext(org.nutz.mvc.ActionContext)

Example 2 with ActionContext

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

the class MappingNodeTest method test_issue.

@Test
public void test_issue() {
    MappingNode<String> root = new MappingNode<String>();
    root.add("/*", "A");
    root.add("/abc/wendal", "B");
    root.add("/abc/wen/?/zzz", "B");
    root.add("/abc/wen/?/zzz/*", "B");
    ActionContext ac = new ActionContext();
    // 连第一个路径都不匹配
    assertEquals("A", root.get(ac, "/a/b/c/d/e"));
    // 匹配第一路径,但不匹配第二路径
    assertEquals("A", root.get(ac, "/abc/abc"));
    // 匹配第一个路径, 也匹配第二路径
    assertEquals("B", root.get(ac, "/abc/wendal"));
    // 匹配全部
    assertEquals("B", root.get(ac, "/abc/wen/qq/zzz"));
    // 最后一个路径不匹配
    assertEquals("A", root.get(ac, "/abc/wen/qq/qqq"));
    // 最后一个路径泛匹配
    assertEquals("B", root.get(ac, "/abc/wen/qq/zzz/123"));
}
Also used : ActionContext(org.nutz.mvc.ActionContext) Test(org.junit.Test)

Example 3 with ActionContext

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

the class MappingNodeTest method test_simple_mapping.

@Test
public void test_simple_mapping() {
    MappingNode<String> root = new MappingNode<String>();
    root.add("/a/b/c", "A");
    root.add("/a/c/", "B");
    root.add("/a/f", "C");
    root.add("/a", "D");
    ActionContext ac = new ActionContext();
    assertEquals("A", root.get(ac, "/a/b/c"));
    assertEquals("/a/b/c", ac.getPath());
    assertEquals("B", root.get(ac, "/a/c"));
    assertEquals("/a/c", ac.getPath());
    assertEquals("C", root.get(ac, "/a/f/"));
    assertEquals("/a/f/", ac.getPath());
    assertEquals("D", root.get(ac, "/a/"));
    assertEquals("/a/", ac.getPath());
    assertNull(root.get(ac, "/a/x"));
    assertEquals("/a/x", ac.getPath());
}
Also used : ActionContext(org.nutz.mvc.ActionContext) Test(org.junit.Test)

Example 4 with ActionContext

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

the class MappingNodeTest method test_remain.

@Test
public void test_remain() {
    MappingNode<String> root = new MappingNode<String>();
    root.add("/a/?/?", "A");
    root.add("/a/**", "B");
    root.add("/a/x", "C");
    root.add("/a/?/m/**", "D");
    ActionContext ac = new ActionContext();
    assertEquals("A", root.get(ac, "/a/b/c"));
    assertEquals("/a/b/c", ac.getPath());
    assertEquals("b,c", Lang.concat(",", ac.getPathArgs()).toString());
    assertEquals("B", root.get(ac, "/a/c/d/e"));
    assertEquals("/a/c/d/e", ac.getPath());
    assertEquals(1, ac.getPathArgs().size());
    assertEquals("c/d/e", ac.getPathArgs().get(0));
    assertEquals("C", root.get(ac, "/a/x/"));
    assertEquals("/a/x/", ac.getPath());
    assertEquals("", Lang.concat(",", ac.getPathArgs()).toString());
    assertEquals("D", root.get(ac, "/a/c/m/x/y/z"));
    assertEquals("/a/c/m/x/y/z", ac.getPath());
    assertEquals(2, ac.getPathArgs().size());
    assertEquals("c", ac.getPathArgs().get(0));
    assertEquals("x/y/z", ac.getPathArgs().get(1));
}
Also used : ActionContext(org.nutz.mvc.ActionContext) Test(org.junit.Test)

Example 5 with ActionContext

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

the class MappingNodeTest method test_multi_path_arg.

@Test
public void test_multi_path_arg() {
    MappingNode<String> root = new MappingNode<String>();
    root.add("/a/*", "A");
    ActionContext ac = new ActionContext();
    assertEquals("A", root.get(ac, "/a"));
    assertEquals(0, ac.getPathArgs().size());
    assertEquals("A", root.get(ac, "/a/b/c"));
    assertEquals(2, ac.getPathArgs().size());
    assertEquals("b", ac.getPathArgs().get(0));
    assertEquals("c", ac.getPathArgs().get(1));
}
Also used : ActionContext(org.nutz.mvc.ActionContext) Test(org.junit.Test)

Aggregations

ActionContext (org.nutz.mvc.ActionContext)9 Test (org.junit.Test)8 HashMap (java.util.HashMap)1 HttpSession (javax.servlet.http.HttpSession)1 Context (org.nutz.lang.util.Context)1 AbstractMvcTest (org.nutz.mvc.AbstractMvcTest)1 AtMap (org.nutz.mvc.config.AtMap)1 ViewProcessor (org.nutz.mvc.impl.processor.ViewProcessor)1