JAVA之:OGNL表达式练习(Mybatis动态SQL习题练习)
一、OGNL表達(dá)式
1.簡(jiǎn)介
OGNL:對(duì)象視圖導(dǎo)航語(yǔ)言. ${user.addr.name} 這種寫法就叫對(duì)象視圖導(dǎo)航。
OGNL不僅僅可以視圖導(dǎo)航.支持比EL表達(dá)式更加豐富的功能。
2.使用OGNL準(zhǔn)備工作
2.1導(dǎo)包
struts2 的包中已經(jīng)包含了.所以不需要導(dǎo)入額外的jar包
2.2代碼準(zhǔn)備
@Test//準(zhǔn)備工作public void fun1() throws Exception{//準(zhǔn)備OGNLContext//準(zhǔn)備RootUser rootUser = new User("tom",18);//準(zhǔn)備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這個(gè)Map作為Context部分 oc.setValues(context);//書寫OGNLOgnl.getValue("", oc, oc.getRoot());
}
登錄后復(fù)制準(zhǔn)備工作
3.基本語(yǔ)法演示
//取出root中user對(duì)象的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);
登錄后復(fù)制取出root中的屬性值
//取出context中鍵為user1對(duì)象的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);
登錄后復(fù)制取出context中的屬性值
//將root中的user對(duì)象的name屬性賦值Ognl.getValue("name='jerry'", oc, oc.getRoot());
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("#user1.name='郝強(qiáng)勇',#user1.name", oc, oc.getRoot());
System.out.println(name);
System.out.println(name2);
登錄后復(fù)制為屬性賦值
//調(diào)用root中user對(duì)象的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);
登錄后復(fù)制調(diào)用方法
String name = (String) Ognl.getValue("@cn.itheima.a_ognl.HahaUtils@echo('hello 強(qiáng)勇!')", 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);
登錄后復(fù)制調(diào)用靜態(tài)方法
//創(chuàng)建list對(duì)象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);*///創(chuàng)建Map對(duì)象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);
登錄后復(fù)制ognl創(chuàng)建對(duì)象-list|map
二、OGNL與Struts2的結(jié)合
1.結(jié)合原理
ValueStack中的兩部分
2.棧原理
棧是由ArrayList模擬的
棧中的兩個(gè)方法的實(shí)現(xiàn)
訪問(wèn)棧中屬性的特點(diǎn).由上到下
3.查看值棧中兩部分內(nèi)容(使用DEBUG標(biāo)簽)
3.1Root
默認(rèn)情況下,棧中放置當(dāng)前訪問(wèn)的Action對(duì)象
3.2Context
Context部分就是ActionContext數(shù)據(jù)中心
4.struts2與ognl結(jié)合體現(xiàn)
4.1參數(shù)接收
如何獲得值棧對(duì)象,值棧對(duì)象與ActionContext對(duì)象是互相引用的
//壓入棧頂//1獲得值棧ValueStack vs = ActionContext.getContext().getValueStack();//2將u壓入棧頂vs.push(u);
登錄后復(fù)制
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><!-- 如果添加的參數(shù)struts"看不懂".就會(huì)作為參數(shù)附加重定向的路徑之后.
如果參數(shù)是動(dòng)態(tài)的.可以使用${}包裹ognl表達(dá)式.動(dòng)態(tài)取值 --><param name="name">${name}</param></result></action>
登錄后復(fù)制語(yǔ)法:${ognl表達(dá)式}
5.擴(kuò)展:request對(duì)象的getAttribute方法
查找順序:
三、練習(xí):客戶列表
public String list() throws Exception {//1 接受參數(shù)String cust_name = ServletActionContext.getRequest().getParameter("cust_name");//2 創(chuàng)建離線查詢對(duì)象DetachedCriteria dc =DetachedCriteria.forClass(Customer.class);//3 判斷參數(shù)拼裝條件if(StringUtils.isNotBlank(cust_name)){
dc.add(Restrictions.like("cust_name", "%"+cust_name+"%"));
}//4 調(diào)用Service將離線對(duì)象傳遞List<Customer> list = cs.getAll(dc);//5 將返回的list放入request域.轉(zhuǎn)發(fā)到list.jsp顯示 //ServletActionContext.getRequest().setAttribute("list", list);// 放到ActionContextActionContext.getContext().put("list", list); return "list";
}
登錄后復(fù)制Action代碼(新增ActionContext存放數(shù)據(jù))
<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> --%>
登錄后復(fù)制JSP顯示數(shù)據(jù)的代碼(注釋的是另一種方法)
注意:<s:iterator value="#list" var="cust" > 每次都會(huì)把cust存在ActionContext中
以上就是JAVA之:OGNL表達(dá)式練習(xí)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注風(fēng)君子博客其它相關(guān)文章!
總結(jié)
以上是生活随笔為你收集整理的JAVA之:OGNL表达式练习(Mybatis动态SQL习题练习)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 的成分的繁荣
- 下一篇: mod函数是什么意思(⊂分别是什么意思)