這篇文章將為大家詳細(xì)講解有關(guān)Spring中怎么利用Expression接口實現(xiàn)表達(dá)式求值操作,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
創(chuàng)新互聯(lián)專注于措美企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,商城建設(shè)。措美網(wǎng)站建設(shè)公司,為措美等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
一 Bean
package org.crazyit.app.domain;import java.util.Date;public class Person{ private Integer id; private String name; private Date birth; // 無參數(shù)的構(gòu)造器 public Person() { } // 初始化全部成員變量的構(gòu)造器 public Person(Integer id , String name , Date birth) { this.id = id; this.name = name; this.birth = birth; } // id的setter和getter方法 public void setId(Integer id) { this.id = id; } public Integer getId() { return this.id; } // name的setter和getter方法 public void setName(String name) { this.name = name; } public String getName() { return this.name; } // birth的setter和getter方法 public void setBirth(Date birth) { this.birth = birth; } public Date getBirth() { return this.birth; }}
二 測試類
package lee;import org.springframework.expression.*;import org.springframework.expression.spel.standard.*;import org.springframework.expression.spel.support.*;import java.util.*;import org.crazyit.app.domain.*;public class SpELTest{ public static void main(String[] args) { // 創(chuàng)建一個ExpressionParser對象,用于解析表達(dá)式 ExpressionParser parser = new SpelExpressionParser(); // 最簡單的字符串表達(dá)式 Expression exp = parser.parseExpression("'HelloWorld'"); System.out.println("'HelloWorld'的結(jié)果: " + exp.getValue()); // 調(diào)用方法的表達(dá)式 exp = parser.parseExpression("'HelloWorld'.concat('!')"); System.out.println("'HelloWorld'.concat('!')的結(jié)果: " + exp.getValue()); // 調(diào)用對象的getter方法 exp = parser.parseExpression("'HelloWorld'.bytes"); System.out.println("'HelloWorld'.bytes的結(jié)果: " + exp.getValue()); // 訪問對象的屬性(d相當(dāng)于HelloWorld.getBytes().length) exp = parser.parseExpression("'HelloWorld'.bytes.length"); System.out.println("'HelloWorld'.bytes.length的結(jié)果:" + exp.getValue()); // 使用構(gòu)造器來創(chuàng)建對象 exp = parser.parseExpression("new String('helloworld')" + ".toUpperCase()"); System.out.println("new String('helloworld')" + ".toUpperCase()的結(jié)果是: " + exp.getValue(String.class)); Person person = new Person(1 , "孫悟空", new Date()); exp = parser.parseExpression("name"); // 以指定對象作為root來計算表達(dá)式的值 // 相當(dāng)于調(diào)用person.name表達(dá)式的值 System.out.println("以persn為root,name表達(dá)式的值是: " + exp.getValue(person , String.class)); exp = parser.parseExpression("name=='孫悟空'"); StandardEvaluationContext ctx = new StandardEvaluationContext(); // 將person設(shè)為Context的root對象 ctx.setRootObject(person); // 以指定Context來計算表達(dá)式的值 System.out.println(exp.getValue(ctx , Boolean.class)); List<Boolean> list = new ArrayList<Boolean>(); list.add(true); EvaluationContext ctx2 = new StandardEvaluationContext(); // 將list設(shè)置成EvaluationContext的一個變量 ctx2.setVariable("list" , list); // 修改list變量的第一個元素的值 parser.parseExpression("#list[0]").setValue(ctx2 , "false"); // list集合的第一個元素被改變 System.out.println("list集合的第一個元素為:" + parser.parseExpression("#list[0]").getValue(ctx2)); }}
三 測試結(jié)果
'HelloWorld'的結(jié)果: HelloWorld'HelloWorld'.concat('!')的結(jié)果: HelloWorld!'HelloWorld'.bytes的結(jié)果: [B@14899482'HelloWorld'.bytes.length的結(jié)果:10new String('helloworld').toUpperCase()的結(jié)果是: HELLOWORLD以persn為root,name表達(dá)式的值是: 孫悟空truelist集合的第一個元素為:false
關(guān)于Spring中怎么利用Expression接口實現(xiàn)表達(dá)式求值操作就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當(dāng)前題目:Spring中怎么利用Expression接口實現(xiàn)表達(dá)式求值操作
網(wǎng)頁URL:http://chinadenli.net/article48/ppcshp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)頁設(shè)計公司、網(wǎng)站內(nèi)鏈、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)