zoukankan      html  css  js  c++  java
  • Spring EL regular expression example

    Spring EL supports regular expression using a simple keyword “matches“, which is really awesome! For examples,

    	@Value("#{'100' matches '\d+' }")
    	private boolean isDigit;
    

    It test whether ‘100‘ is a valid digit via regular expression ‘\d+‘.

    Spring EL in Annotation

    See following Spring EL regular expression examples, some mixed with ternary operator, which makes Spring EL pretty flexible and powerful.

    Below example should be self-explanatory.

    package com.mkyong.core;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("customerBean")
    public class Customer {
    
    	// email regular expression
    	String emailRegEx = "^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)" +
    			"*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";
    
    	// if this is a digit?
    	@Value("#{'100' matches '\d+' }")
    	private boolean validDigit;
    
    	// if this is a digit + ternary operator
    	@Value("#{ ('100' matches '\d+') == true ? " +
    			"'yes this is digit' : 'No this is not a digit'  }")
    	private String msg;
    
    	// if this emailBean.emailAddress contains a valid email address?
    	@Value("#{emailBean.emailAddress matches customerBean.emailRegEx}")
    	private boolean validEmail;
    
    	//getter and setter methods, and constructor	
    }
    
    package com.mkyong.core;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("emailBean")
    public class Email {
    
    	@Value("nospam@abc.com")
    	String emailAddress;
    
    	//...
    }
    

    Output

    Customer [isDigit=true, msg=yes this is digit, isValidEmail=true]
    

    Spring EL in XML

    See equivalent version in bean definition XML file.

    <beans xmlns="http://www.springframework.org/schema/beans"
    	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-3.0.xsd">
    
    	<bean id="customerBean" class="com.mkyong.core.Customer">
    	  <property name="validDigit" value="#{'100' matches 'd+' }" />
    	  <property name="msg"
    		value="#{ ('100' matches 'd+') == true ? 'yes this is digit' : 'No this is not a digit'  }" />
    	  <property name="validEmail"
    		value="#{emailBean.emailAddress matches '^[_A-Za-z0-9-]+(.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,})$' }" />
    	</bean>
    
    	<bean id="emailBean" class="com.mkyong.core.Email">
    	  <property name="emailAddress" value="nospam@abc.com" />
    	</bean>
    
    </beans>
    
  • 相关阅读:
    JS调用摄像头拍照,编辑jscam.swf自定义清晰度,兼容IE
    Hbase 永久 Region-In-Transition 的查错记录
    hbase meta中分区信息错误的记录
    spark aggregateByKey 时 java.lang.OutOfMemoryError: GC overhead limit exceeded
    在 aws emr 上,将 hbase table A 的数据,对 key 做 hash,写到另外一张 table B
    EMR 配置纪录(不断更新)
    使用 JvisualVM 监控 spark executor
    YARN 集群的资源分配
    spark bulkload hbase笔记
    记一个 protobuf 的 jar 包冲突
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4749622.html
Copyright © 2011-2022 走看看