Struts2.1 ONGL表達式

ActionContext context # #parmeters['參數(shù)'] request.getParameter(參數(shù)); #parmeters.參數(shù) #request['屬性']request.getAttribute(); #request.屬性 request.屬性=valuerequest.setAttribute(key,value); request['屬性']=value #session['屬性']session.getAttribute(); #session.屬性 #application['屬性']application.getAttribute(); #application.屬性 #屬性page/request/session/application [HttpRequest]----[Struts2]----ActionContext context\valueStack\action ActionContext context= ActionContext.getContext(); Map session=context.getSession(); session.put("nam1","jim"); ===>#session['nam1'] ActionContext context= ActionContext.getContext(); context.put("nam2","tom");===>#nam2 abc def ghi ddd 123 456 789 111 222 666 hhh jjjj kkkk============================================================================Struts2.1 ONGL 表達式Java代碼 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="com.rao.struts2.bean.Sex"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% request.setAttribute("req", "request scope"); request.getSession().setAttribute("sess", "session scope"); request.getSession().getServletContext().setAttribute("app", "aplication scope"); %> 1.通過ongl表達式獲取 屬性范圍中的值

2.通過ongl表達式創(chuàng)建list 集合 ,并且遍歷出集合中的值

3.通過ongl表達式創(chuàng)建Map 集合 ,并且遍歷出集合中的值

->

4.通過ongl表達式 進行邏輯判斷


? ? ? ? ? ? aa 在 集合{'aaa','bbb'}中;?


? ? ? ? ? ? aa 不在 集合{'aaa','bbb'}中;?



? ? ? ? ? ? 不 在 集合list中;?


? ? ? ? ? ? 在 集合list中;?


5.通過ongl表達式 的投影功能進行數(shù)據(jù)篩選

${o }

5.通過ongl表達式 訪問某個類的靜態(tài)方法和值

6.ongl表達式 迭代標簽 詳細


? ? ? ? ? ? ? ? pink">


? ? ? ? ? ? ? ? ? ? 索引?


? ? ? ? ? ? ? ? ? ? 值?


? ? ? ? ? ? ? ? ? ? 奇??


? ? ? ? ? ? ? ? ? ? 偶??


? ? ? ? ? ? ? ? ? ? 首??


? ? ? ? ? ? ? ? ? ? 尾??


? ? ? ? ? ? ? ? ? ? 當前迭代數(shù)量?




? ? ? ? ? ? ? ? ? ? YNYNYNYN?


7.ongl表達式:? if/else if/else 詳細

<% request.setAttribute("aa",0); %>?

? ? ? ? ? ? 在0-4之間;?


? ? ? ? ? ? 在4-8之間;?


? ? ? ? ? ? 大于8;?


8.ongl表達式: url 詳細

<% request.setAttribute("aa","sss"); %>100

value以字符處理:

value明確指定以O(shè)NGL表達式處理:

9.ongl表達式: checkboxlist 詳細

1> .list 生成;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

name:checkboxlist的名字

list:checkboxlist要顯示的列表

value:checkboxlist默認被選中的選項,checked=checked

以上生成代碼:

? ? ? ? ? ? ? 上網(wǎng)? ? ? ? ? ? ? ? ? ? ? ? ? ? 看書? ? ? ? ? ? ? ? ? ? ? ? ? ? 爬山? ? ? ? ? ? ? ? ? ? ? ? ? ? 游泳? ? ? ? ? ? ? ? ? ? ? ? ? ? 唱歌"2> .Map 生成;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

name:checkboxlist的名字

list:checkboxlist要顯示的列表

listKey:checkbox 的value的值

listValue:checkbox 的lablel(顯示的值)

value:checkboxlist默認被選中的選項,checked=checked

以上生成代碼:

? ? ? ? ? ? ? 上網(wǎng)? ? ? ? ? ? ? ? ? ? ? ? ? ? 看書? ? ? ? ? ? ? ? ? ? ? ? ? ? 爬山? ? ? ? ? ? ? ? ? ? ? ? ? ? 游泳? ? ? ? ? ? ? ? ? ? ? ? ? ? 唱歌

10.ongl表達式: s:radio 詳細

<%? ? ? ? ? ? ? Sex sex1 = new Sex(1,"男");? ? ? ? ? ? ? ? Sex sex2 = new Sex(2,"女");? ? ? ? ? ? ? Listlist = new ArrayList();? ? ? ? ? ? ? ? list.add(sex1);? ? ? ? ? ? ? list.add(sex2);? ? ? ? ? ? ? request.setAttribute("sexs",list);? ? ? ? ? ? %>? ? ? ? ? 這個與checkboxlist差不多;

1>.如果集合為javabean:

2>.如果集合為list:

3>.如果集合為map:

11.ongl表達式: s:select 詳細

這個與s:checkboxlist差不多;

1>.如果集合為javabean:

2>.如果集合為list:

3>.如果集合為map:

到此主要的ONGL標簽已經(jīng)介紹完畢...由于表單標簽相對簡單不介紹了....? ? ? ? ? ? =============================================================================? OGNL表達式的基本語法和用法? 首先我們一起來看一下OGNL中的#、%和$符號。一.OGNL中的#、%和$符號? ? ? #、%和$符號在OGNL表達式中經(jīng)常出現(xiàn),而這三種符號也是開發(fā)者不容易掌握和理解的部分。在這里我們簡單介紹它們的相應用途。1.#符號的三種用法? 1)訪問非根對象屬性,例如示例中的#session.msg表達式,由于Struts 2中值棧被視為根對象,所以訪問其他非根對象時,需要加#前綴。實際上,#相當于ActionContext. getContext();#session.msg表達式相當于ActionContext.getContext().getSession(). getAttribute("msg") 。? 2)用于過濾和投影(projecting)集合,如示例中的persons.{?#this.age>20}。? 3) 用來構(gòu)造Map,例如示例中的#{'foo1':'bar1', 'foo2':'bar2'}。2.%符號? ? ? %符號的用途是在標志的屬性為字符串類型時,計算OGNL表達式的值。如下面的代碼所示:

構(gòu)造Map

The value of key "foo1" is

不使用%:

使用%:

運行界面如下所示。he value of key "foo1" is bar1不使用%:#foobar['foo1']使用%:bar13.$符號$符號主要有兩個方面的用途。? ? 1) 在國際化資源文件中,引用OGNL表達式,例如國際化資源文件中的代碼:reg.agerange=國際化資源信息:年齡必須在${min}同${max}之間。? ? 2) 在Struts 2框架的配置文件中引用OGNL表達式,例如下面的代碼片斷所示:10100BAction-test校驗:數(shù)字必須為${min}為${max}之間!二.我們一起看一下OGNL常用表達式: 1. 當使用OGNL調(diào)用靜態(tài)方法的時候,需要按照如下語法編寫表達式: @package.classname@methodname(parameter) 2. 對于OGNL來說,java.lang.Math是其的默認類,如果調(diào)用java.lang.Math的靜態(tài)方法時,無需指定類的名字,比如:@@min(4, 10); 3. 對于OGNL來說,數(shù)組與集合是一樣的,都是通過下標索引來去訪問的。獲取List:

獲取List中的某一個元素(可以使用類似于數(shù)組中的下標獲取List中的內(nèi)容):

獲取Set:

獲取Set中的某一個元素(Set由于沒有順序,所以不能使用下標獲取數(shù)據(jù)):

×獲取Map:

獲取Map中所有的鍵:

獲取Map中所有的值:

獲取Map中的某一個元素(可以使用類似于數(shù)組中的下標獲取List中的內(nèi)容):

獲取List的大小:

4. 使用OGNL來處理映射(Map)的語法格式如下所示:? #{‘key1’: ‘value1’, ‘key2’: ‘value2’, ‘key3’: ‘value3’}; 5. 過濾(filtering):collection.{? expression} 6. OGNL針對集合提供了一些偽屬性(如size,isEmpty),讓我們可以通過屬性的方式來調(diào)用方法(本質(zhì)原因在于集合當中的很多方法并不符合JavaBean的命名規(guī)則),但我么你依然還可以通過調(diào)用方法來實現(xiàn)與偽屬性相同的目的。 7. 過濾(filtering),獲取到集合中的第一個元素:collection.{^ expression} 8. 過濾(filtering),獲取到集合中的最后一個元素:collection.{& expression} 9. 在使用過濾操作時,我們通常都會使用#this,該表達式用于代表當前正在迭代的集合中的對象(聯(lián)想增強的for循環(huán)) 10. 投影(projection):collection.{expression} 11. 過濾與投影之間的差別:類比于數(shù)據(jù)庫中的表,過濾是取行的操作,而投影是取列的操作。 具體舉例如下:利用選擇獲取List中成績及格的對象:

利用選擇獲取List中成績及格的對象的username:

利用選擇獲取List中成績及格的第一個對象的username:

利用選擇獲取List中成績及格的第一個對象的username:

利用選擇獲取List中成績及格的最后一個對象的username:

利用選擇獲取List中成績及格的第一個對象然后求大小:

12. 在Struts2中,根對象就是ValueStack。在Struts2的任何流程當中,ValueStack中的最頂層對象一定是Action對象。? 13. parameters,#parameters.username request, #request.username session, #session.username application, #application.username attr, #attr.username 以上幾個對象叫做“命名對象”。 14. 訪問靜態(tài)方法或是靜態(tài)成員變量的改進。 @vs@method 15. 關(guān)于Struts2標簽庫屬性值的%與#號的關(guān)系: 1). 如果標簽的屬性值是OGNL表達式,那么無需加上%{}。 2). 如果標簽的屬性值是字符串類型,那么在字符串當中凡是出現(xiàn)的%{}都會被解析成OGNL表達式,解析完畢后再與其他的字符串進行拼接構(gòu)造出最后的字符串值。 3). 我們可以在所有的屬性值上加%{},這樣如果該屬性值是OGNL表達式,那么標簽處理類就會將%{}忽略掉。 最后一起用代碼說話,簡單的看一下ognl操作的示例:1)上下文環(huán)境中使用OGNLpublic static void main(String[] args)? ? {? ? ? ? ? ? ? Mapcontext = new HashMap();

? ? ? ? Person person1 = new Person();

? ? ? ? person1.setName("zhangsan");


? ? ? ? Person person2 = new Person();

? ? ? ? person2.setName("lisi");

? ? ? ? Person person3 = new Person();

? ? ? ? person3.setName("wangwu");


? ? ? ? Person person4 = new Person();

? ? ? ? person4.setName("zhaoliu");


? ? ? ? context.put("person1", person1);

? ? ? ? context.put("person2", person2);

? ? ? ? context.put("person3", person3);

? ? ? ? try

? ? ? ? {


? ? ? ? ? ? Object value = Ognl.getValue("name", context, person2);

? ? ? ? ? ? System.out.println("ognl expression \"name\" evaluation is : " + value);


? ? ? ? ? ? Object value2 = Ognl.getValue("#person2.name", context, person2);

? ? ? ? ? ? System.out.println("ognl expression \"#person2.name\" evaluation is : " + value2);


? ? ? ? ? ? Object value3 = Ognl.getValue("#person1.name", context, person2);

? ? ? ? ? ? System.out.println("ognl expression \"#person1.name\" evaluation is : " + value3);


? ? ? ? ? ? Object value4 = Ognl.getValue("name", context, person4);

? ? ? ? ? ? System.out.println("ognl expression \"name\" evaluation is : " + value4);


? ? ? ? ? ? Object value5 = Ognl.getValue("#person4.name", context, person4);

? ? ? ? ? ? System.out.println("ognl expression \"person4.name\" evaluation is : " + value5);


? ? ? ? ? ? // Object value6 = Ognl.getValue("#person4.name", context, person2);

? ? ? ? ? // System.out.println("ognl expression \"#person4.name\" evaluation is : " + value6);

? ? ? ? }

2)使用OGNL調(diào)用方法

public static void main(String[] args)

? ? {


? ? ? ? OgnlContext context = new OgnlContext();

? ? ? ? People people1 = new People();

? ? ? ? people1.setName("zhangsan");

? ? ? ? People people2 = new People();

? ? ? ? people2.setName("lisi");

? ? ? ? People people3 = new People();

? ? ? ? people3.setName("wangwu");

? ? ? ? context.put("people1", people1);

? ? ? ? context.put("people2", people2);

? ? ? ? context.put("people3", people3);


? ? ? ? context.setRoot(people1);

? ? ? ? try

? ? ? ? {


? ? ? ? ? ? Object value = Ognl.getValue("name.length()", context, context.getRoot());

? ? ? ? ? ? System.out.println("people1 name length is :" + value);


? ? ? ? ? ? Object upperCase = Ognl.getValue("#people2.name.toUpperCase()", context, context.getRoot());

? ? ? ? ? ? System.out.println("people2 name upperCase is :" + upperCase);

? ? ? ? ? ? Object invokeWithArgs = Ognl.getValue("name.charAt(5)", context, context.getRoot());

? ? ? ? ? ? System.out.println("people1 name.charAt(5) is :" + invokeWithArgs);


? ? ? ? ? ? Object min = Ognl.getValue("@java.lang.Math@min(4,10)", context, context.getRoot());

? ? ? ? ? ? System.out.println("min(4,10) is :" + min);


? ? ? ? ? ? Object e = Ognl.getValue("@java.lang.Math@E", context, context.getRoot());

? ? ? ? ? ? System.out.println("E is :" + e);

? ? ? ? }

3)使用OGNL操作集合

public static void main(String[] args) throws Exception

? ? {

? ? ? ? OgnlContext context = new OgnlContext();


? ? ? ? Classroom classroom = new Classroom();

? ? ? ? classroom.getStudents().add("zhangsan");

? ? ? ? classroom.getStudents().add("lisi");

? ? ? ? classroom.getStudents().add("wangwu");

? ? ? ? classroom.getStudents().add("zhaoliu");

? ? ? ? classroom.getStudents().add("qianqi");


? ? ? ? Student student = new Student();

? ? ? ? student.getContactWays().put("homeNumber", "110");

? ? ? ? student.getContactWays().put("companyNumber", "119");

? ? ? ? student.getContactWays().put("mobilePhone", "112");


? ? ? ? context.put("classroom", classroom);

? ? ? ? context.put("student", student);

? ? ? ? context.setRoot(classroom);


? ? ? ? Object collection = Ognl.getValue("students", context, context.getRoot());

? ? ? ? System.out.println("students collection is :" + collection);


? ? ? ? Object firstStudent = Ognl.getValue("students[0]", context, context.getRoot());

? ? ? ? System.out.println("first student is : " + firstStudent);


? ? ? ? Object size = Ognl.getValue("students.size()", context, context.getRoot());

? ? ? ? System.out.println("students collection size is :" + size);

? ? ? ? System.out.println("--------------------------飄逸的分割線--------------------------");


? ? ? ? Object mapCollection = Ognl.getValue("#student.contactWays", context, context.getRoot());

? ? ? ? System.out.println("mapCollection is :" + mapCollection);

? ? ? ? Object firstElement = Ognl.getValue("#student.contactWays['homeNumber']", context, context.getRoot());

? ? ? ? System.out.println("the first element of contactWays is :" + firstElement);

? ? ? ? System.out.println("--------------------------飄逸的分割線--------------------------");


? ? ? ? Object createCollection = Ognl.getValue("{'aa','bb','cc','dd'}", context, context.getRoot());

? ? ? ? System.out.println(createCollection);


? ? ? ? Object createMapCollection = Ognl.getValue("#{'key1':'value1','key2':'value2'}", context, context.getRoot());

? ? ? ? System.out.println(createMapCollection);

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容