宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

一、OGNL表达式

  1.简介

  OGNL:对象视图导航语言. ${user.addr.name} 这种写法就叫对象视图导航。
  OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能。

  2.使用OGNL准备工作

   2.1导包

  struts2 的包中已经包含了.所以不需要导入额外的jar包

   2.2代码准备

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

    @Test//准备工作public void fun1) throws Exception{//准备OGNLContext//准备RootUser rootUser = new User"tom",18);//准备ContextMap<String,User> context = new HashMap<String,User>);
            context.put"user1", new User"jack",18));
            context.put"user2", new User"rose",22));
        OgnlContext oc = new OgnlContext);//将rootUser作为root部分        oc.setRootrootUser);//将context这个Map作为Context部分        oc.setValuescontext);//书写OGNLOgnl.getValue"", oc, oc.getRoot));
    }

准备工作

  3.基本语法演示

JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

        //取出root中user对象的name属性String name = String) Ognl.getValue"name", oc, oc.getRoot));
        Integer age = Integer) Ognl.getValue"age", oc, oc.getRoot));
        System.out.printlnname);
        System.out.printlnage);

取出root中的属性值JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

        //取出context中键为user1对象的name属性String name = String) Ognl.getValue"#user1.name", oc, oc.getRoot));
        String name2 = String) Ognl.getValue"#user2.name", oc, oc.getRoot));
        Integer age = Integer) Ognl.getValue"#user2.age", oc, oc.getRoot));
        System.out.printlnname);
        System.out.printlnname2);
        System.out.printlnage);

取出context中的属性值JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

        //将root中的user对象的name属性赋值Ognl.getValue"name='jerry'", oc, oc.getRoot));
        String name = String) Ognl.getValue"name", oc, oc.getRoot));
        
        String name2 = String) Ognl.getValue"#user1.name='郝强勇',#user1.name", oc, oc.getRoot));
        System.out.printlnname);
        System.out.printlnname2);

为属性赋值JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

        //调用root中user对象的setName方法Ognl.getValue"setName'lilei')", oc, oc.getRoot));
        String name = String) Ognl.getValue"getName)", oc, oc.getRoot));
        String name2 = String) Ognl.getValue"#user1.setName'lucy'),#user1.getName)", oc, oc.getRoot));
        
        System.out.printlnname);
        System.out.printlnname2);

调用方法JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

        String name = String) Ognl.getValue"@cn.itheima.a_ognl.HahaUtils@echo'hello 强勇!')", oc, oc.getRoot));//Double pi = Double) Ognl.getValue"@java.lang.Math@PI", oc, oc.getRoot));Double pi = Double) Ognl.getValue"@@PI", oc, oc.getRoot));
        System.out.printlnname);
        System.out.printlnpi);

调用静态方法JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

        //创建list对象Integer size = Integer) Ognl.getValue"{'tom','jerry','jack','rose'}.size)", oc, oc.getRoot));
        String name = String) Ognl.getValue"{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot));
        String name2 = String) Ognl.getValue"{'tom','jerry','jack','rose'}.get1)", oc, oc.getRoot));    /*System.out.printlnsize);
        System.out.printlnname);
        System.out.printlnname2);*///创建Map对象Integer size2 = Integer) Ognl.getValue"#{'name':'tom','age':18}.size)", oc, oc.getRoot));
        String name3  = String) Ognl.getValue"#{'name':'tom','age':18}['name']", oc, oc.getRoot));
        Integer age  = Integer) Ognl.getValue"#{'name':'tom','age':18}.get'age')", oc, oc.getRoot));
        System.out.printlnsize2);
        System.out.printlnname3);
        System.out.printlnage);

ognl创建对象-list|map

二、OGNL与Struts2的结合

  1.结合原理

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

  ValueStack中的两部分

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

  2.栈原理

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

  栈是由ArrayList模拟的

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

  栈中的两个方法的实现

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

  访问栈中属性的特点.由上到下

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

  3.查看值栈中两部分内容使用DEBUG标签)

   3.1Root

  默认情况下,栈中放置当前访问的Action对象

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

  3.2Context

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

  Context部分就是ActionContext数据中心

  4.struts2与ognl结合体现

   4.1参数接收

JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客 

JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

    如何获得值栈对象,值栈对象与ActionContext对象是互相引用的

        //压入栈顶//1获得值栈ValueStack vs = ActionContext.getContext).getValueStack);//2将u压入栈顶vs.pushu);

   4.2配置文件中

JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

<action name="Demo3Action" class="cn.itheima.d_config.Demo3Action" method="execute" ><result name="success" type="redirectAction" ><param name="actionName">Demo1Action</param><param name="namespace">/</param><!-- 如果添加的参数struts"看不懂".就会作为参数附加重定向的路径之后.
                     如果参数是动态的.可以使用${}包裹ognl表达式.动态取值                 --><param name="name">${name}</param></result></action>

语法:${ognl表达式}

  5.扩展:request对象的getAttribute方法

   查找顺序:

  JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

三、练习:客户列表

JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

    public String list) throws Exception {//1 接受参数String cust_name = ServletActionContext.getRequest).getParameter"cust_name");//2 创建离线查询对象DetachedCriteria dc =DetachedCriteria.forClassCustomer.class);//3 判断参数拼装条件ifStringUtils.isNotBlankcust_name)){
            dc.addRestrictions.like"cust_name", "%"+cust_name+"%"));
        }//4 调用Service将离线对象传递List<Customer> list = cs.getAlldc);//5 将返回的list放入request域.转发到list.jsp显示        //ServletActionContext.getRequest).setAttribute"list", list);// 放到ActionContextActionContext.getContext).put"list", list);        return "list";
    }

Action代码(新增ActionContext存放数据)JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

<s:iterator value="#list" var="cust" >
    <TR         
        style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
        <TD>
            <s:property value="#cust.cust_name" />
        </TD>
        <TD>
        <s:property value="#cust.cust_level" />
        </TD>
        <TD>
        <s:property value="#cust.cust_source" />
        </TD>
        <TD>
        <s:property value="#cust.cust_linkman" />
        </TD>
        <TD>
        <s:property value="#cust.cust_phone" />
        </TD>
        <TD>
        <s:property value="#cust.cust_mobile" />
        </TD>
        <TD>
        <a href="${pageContext.request.contextPath }/customerServlet?method=edit&custId=${customer.cust_id}">修改</a>
        &nbsp;&nbsp;<a href="${pageContext.request.contextPath }/customerServlet?method=delete&custId=${customer.cust_id}">删除</a>
        </TD>
    </TR>
    </s:iterator>
    <%-- <s:iterator value="#list"  >
    <TR         
        style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
        <TD>
            <s:property value="cust_name" />
        </TD>
        <TD>
        <s:property value="cust_level" />
        </TD>
        <TD>
        <s:property value="cust_source" />
        </TD>
        <TD>
        <s:property value="cust_linkman" />
        </TD>
        <TD>
        <s:property value="cust_phone" />
        </TD>
        <TD>
        <s:property value="cust_mobile" />
        </TD>
        <TD>
        <a href="${pageContext.request.contextPath }/customerServlet?method=edit&custId=${customer.cust_id}">修改</a>
        &nbsp;&nbsp;<a href="${pageContext.request.contextPath }/customerServlet?method=delete&custId=${customer.cust_id}">删除</a>
        </TD>
    </TR>
    </s:iterator> --%>

JSP显示数据的代码(注释的是另一种方法)

注意:<s:iterator value="#list" var="cust" > 每次都会把cust存在ActionContext中

JAVA之:OGNL表达式练习Mybatis动态SQL习题练习-风君子博客

以上就是JAVA之:OGNL表达式练习的详细内容,更多请关注风君子博客其它相关文章!