日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

JAVA之:OGNL表达式练习(Mybatis动态SQL习题练习)

發布時間:2023/12/15 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 JAVA之:OGNL表达式练习(Mybatis动态SQL习题练习) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、OGNL表達式

  1.簡介

  OGNL:對象視圖導航語言. ${user.addr.name} 這種寫法就叫對象視圖導航。
  OGNL不僅僅可以視圖導航.支持比EL表達式更加豐富的功能。

  2.使用OGNL準備工作

   2.1導包

  struts2 的包中已經包含了.所以不需要導入額外的jar包

   2.2代碼準備

  

    @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.setRoot(rootUser);//將context這個Map作為Context部分        oc.setValues(context);//書寫OGNLOgnl.getValue("", oc, oc.getRoot());
    }

登錄后復制準備工作

  3.基本語法演示

        //取出root中user對象的name屬性String name = (String) Ognl.getValue("name", oc, oc.getRoot());
        Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(age);

登錄后復制取出root中的屬性值

        //取出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.println(name);
        System.out.println(name2);
        System.out.println(age);

登錄后復制取出context中的屬性值

        //將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.println(name);
        System.out.println(name2);

登錄后復制為屬性賦值

        //調用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.println(name);
        System.out.println(name2);

登錄后復制調用方法

        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.println(name);
        System.out.println(pi);

登錄后復制調用靜態方法

        //創建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'}.get(1)", oc, oc.getRoot());    /*System.out.println(size);
        System.out.println(name);
        System.out.println(name2);*///創建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.println(size2);
        System.out.println(name3);
        System.out.println(age);

登錄后復制ognl創建對象-list|map

二、OGNL與Struts2的結合

  1.結合原理

  

  ValueStack中的兩部分

  

  2.棧原理

  

  棧是由ArrayList模擬的

  

  棧中的兩個方法的實現

  

  訪問棧中屬性的特點.由上到下

  

  3.查看值棧中兩部分內容(使用DEBUG標簽)

   3.1Root

  默認情況下,棧中放置當前訪問的Action對象

  

  3.2Context

  

  

  Context部分就是ActionContext數據中心

  4.struts2與ognl結合體現

   4.1參數接收

 

    如何獲得值棧對象,值棧對象與ActionContext對象是互相引用的

        //壓入棧頂//1獲得值棧ValueStack vs = ActionContext.getContext().getValueStack();//2將u壓入棧頂vs.push(u);

登錄后復制

   4.2配置文件中

<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方法

   查找順序:

  

三、練習:客戶列表

    public String list() throws Exception {//1 接受參數String cust_name = ServletActionContext.getRequest().getParameter("cust_name");//2 創建離線查詢對象DetachedCriteria dc =DetachedCriteria.forClass(Customer.class);//3 判斷參數拼裝條件if(StringUtils.isNotBlank(cust_name)){
            dc.add(Restrictions.like("cust_name", "%"+cust_name+"%"));
        }//4 調用Service將離線對象傳遞List<Customer> list = cs.getAll(dc);//5 將返回的list放入request域.轉發到list.jsp顯示        //ServletActionContext.getRequest().setAttribute("list", list);// 放到ActionContextActionContext.getContext().put("list", list);        return "list";
    }

登錄后復制Action代碼(新增ActionContext存放數據)

<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表達式練習的詳細內容,更多請關注風君子博客其它相關文章!

總結

以上是生活随笔為你收集整理的JAVA之:OGNL表达式练习(Mybatis动态SQL习题练习)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。