欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Spring中怎么利用Expression接口實(shí)現(xiàn)表達(dá)式求值操作

這篇文章將為大家詳細(xì)講解有關(guān)Spring中怎么利用Expression接口實(shí)現(xiàn)表達(dá)式求值操作,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)專注于措美企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),商城建設(shè)。措美網(wǎng)站建設(shè)公司,為措美等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(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;   // 無(wú)參數(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;   }}

二 測(cè)試類

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)建一個(gè)ExpressionParser對(duì)象,用于解析表達(dá)式    ExpressionParser parser = new SpelExpressionParser();    // 最簡(jiǎn)單的字符串表達(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)用對(duì)象的getter方法    exp = parser.parseExpression("'HelloWorld'.bytes");    System.out.println("'HelloWorld'.bytes的結(jié)果: "      + exp.getValue());    // 訪問(wèn)對(duì)象的屬性(d相當(dāng)于HelloWorld.getBytes().length)    exp = parser.parseExpression("'HelloWorld'.bytes.length");    System.out.println("'HelloWorld'.bytes.length的結(jié)果:"      + exp.getValue());    // 使用構(gòu)造器來(lái)創(chuàng)建對(duì)象    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");    // 以指定對(duì)象作為root來(lái)計(jì)算表達(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對(duì)象    ctx.setRootObject(person);    // 以指定Context來(lái)計(jì)算表達(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的一個(gè)變量    ctx2.setVariable("list" , list);    // 修改list變量的第一個(gè)元素的值    parser.parseExpression("#list[0]").setValue(ctx2 , "false");    // list集合的第一個(gè)元素被改變    System.out.println("list集合的第一個(gè)元素為:"      + parser.parseExpression("#list[0]").getValue(ctx2));  }}

三 測(cè)試結(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集合的第一個(gè)元素為:false

關(guān)于Spring中怎么利用Expression接口實(shí)現(xiàn)表達(dá)式求值操作就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前題目:Spring中怎么利用Expression接口實(shí)現(xiàn)表達(dá)式求值操作
網(wǎng)頁(yè)URL:http://chinadenli.net/article48/ppcshp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化網(wǎng)頁(yè)設(shè)計(jì)公司網(wǎng)站內(nèi)鏈網(wǎng)站制作品牌網(wǎng)站建設(shè)用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)