zoukankan      html  css  js  c++  java
  • 使用SpringEL表达式进行三目运算

    原文链接:http://www.yiidian.com/spring/spring-el-three-mesh-operator.html

    SpEL支持三目运算符,以此来实现条件语句。

    一、编写Bean类

    Item类:

    package com.yiidian.domain;
    /**
     * 
     * @author http://www.yiidian.com
     *
     */
    public class Item {
    	
    	private int qtyOnHand;
        public int getQtyOnHand() {
            return qtyOnHand;
        }
     
        public void setQtyOnHand(int qtyOnHand) {
            this.qtyOnHand = qtyOnHand;
        }
    }
    

    Customer类:

    package com.yiidian.domain;
    
    import java.io.Serializable;
    /**
     * 
     * @author http://www.yiidian.com
     *
     */
    public class Customer implements Serializable{
        private boolean warning;
        public boolean isWarning() {
            return warning;
        }
        public void setWarning(boolean warning) {
            this.warning = warning;
        }
     
        @Override
        public String toString() {
            return "Customer [warning=" + warning + "]";
        }
    }
    

    二、配置applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    	
    	<bean id="customer" class="com.yiidian.domain.Customer">
            <property name="warning" value="#{itemBean.qtyOnHand &lt; 100 ? true : false}" />
        </bean>
     
        <bean id="itemBean" class="com.yiidian.domain.Item">
            <property name="qtyOnHand" value="99" />
        </bean>
    
    
    </beans>
    

    三、编写测试类

    package com.yiidian.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.yiidian.domain.Customer;
    
    /**
     * @author http://www.yiidian.com
     * 
     */
    public class Demo1 {
    
    	@Test
    	public void test1() {
    		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    		Customer customer = (Customer)context.getBean("customer");
    		System.out.println(customer);
    	}
    
    }
    

    四、运行结果

    file

    源码下载:http://pan.baidu.com/s/1qYmLKfE
    file

    欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。
    如果您对我的系列教程感兴趣,也可以关注我的网站:yiidian.com

  • 相关阅读:
    内存使用信息及cpu使用信息
    网站被攻击了怎么办
    seo 百度不收录
    php 使用功能
    sl 动态调用wcf
    php 项目中遇到的问题 ...
    Closures
    php 配置虚拟主机
    jQery 常用工具大全
    jquery基础使用!
  • 原文地址:https://www.cnblogs.com/yiidian/p/12490446.html
Copyright © 2011-2022 走看看