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>
<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>
<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习题练习)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 的成分的繁荣
- 下一篇: mod函数是什么意思(⊂分别是什么意思)