Spring EL ternary operator(if-then-else)example
2313 ワード
Spring EL supports ternary operator,perform“if then else”conditional checting.For example,
Spring EL ternary operatowith
condition ? true : false
Spring EL in AnnotationSpring EL ternary operatowith
@Value
annotation.In this example,if「itemBean.qtyOnHand
」is less than 100,then set「customerBean.warning
」to true
,else set it to false
.package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("customerBean")
public class Customer {
@Value("#{itemBean.qtyOnHand < 100 ? true : false}")
private boolean warning;
public boolean isWarning() {
return warning;
}
public void setWarning(boolean warning) {
this.warning = warning;
}
@Override
public String toString() {
return "Customer [warning=" + warning + "]";
}
}
package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("itemBean")
public class Item {
@Value("99")
private int qtyOnHand;
public int getQtyOnHand() {
return qtyOnHand;
}
public void setQtyOnHand(int qtyOnHand) {
this.qtyOnHand = qtyOnHand;
}
}
OutputCustomer [warning=true]
Spring EL in XML See equivalent version in bean definition XML file.
OutputCustomer [warning=true]
In XML,you need to replace less than operator“<
”with“<
”.