Chapter 1 Common EL expressions and chapter 1 el expressions

Source: Internet
Author: User

Chapter 1 Common EL expressions and chapter 1 el expressions

ElMost commonIn the following scenarios:

  • Read attributes from the configuration file
  • If the value is missing, configure the default value.
  • El internal String use String method
  • Three-object Operator
  • Regular Expression
  • System properties)
  • Call Original system functions
  • Direct file injection
  • Read the return value of another bean Function

1. Read attributes from the configuration file

Application. properties

name=\u8D75\u8BA1\u521A
1 @ Value ("$ {name}") // $ must be used here. # is not applicable. 2 private String name; 3 @ RequestMapping (value = "/name ", method = RequestMethod. GET) 4 public String getName () {5 return name; 6}

Note: Remember the following two sentences.

  • $ {} Reads the attribute file value
  • $ {} Reads the last value that meets the condition; # {} reads all values that meet the condition.

2. If the value is missing, configure the default value.

1 @ Value ("$ {name2: 'hangzhou'}") // $ must be used here. #2 private String name2 is not supported; 3 @ RequestMapping (value = "/name2", method = RequestMethod. GET) 4 public String getName2 () {5 return name2; 6}

3. Use the String method for el internal strings

1 name.list=\u8D75\u8BA1\u521A,\u738B\u5A1C
1     @Value("#{'${name.list}'.split(',')}")2     private List<String> nameList;3     @RequestMapping(value = "/nameList", method = RequestMethod.GET)4     public List<String> getNameList() {5         return nameList;6     }

4. Three-object Operator

1 name.three=\u6768\u8FC7
1 @ RequestMapping (value = "/nameThree", method = RequestMethod. GET) 2 public String getNameThree (@ Value ("$ {name. three! = 'Yang Ta '? 'Yellow Rong ': 'Dragon female'} ") String nameThree) {3 return nameThree; 4}

Note: @ value can be directly used as an input parameter.

5. Regular Expression

1 @ Value ("# {'000000' matches '\ d +'}") // # is required here. 2 private boolean isDigital cannot be used with $; 3 @ RequestMapping (value = "/nameRegEx", method = RequestMethod. GET) 4 public boolean getNameRegEx () {5 return isDigital; 6}

6. Inject system attributes

1 @ Value ("# {systemProperties ['OS. name']} ") // # Must be used here. $ cannot be used. 2 private String osName; 3 @ RequestMapping (value ="/osName ", method = RequestMethod. GET) 4 public String getOsName () {5 return osName; 6}

7. Call Original system functions

1 @ Value ("# {T (java. lang. math ). random () * 10} ") // Note: This is generated only once. No matter how many times you call getRandomValue (), the same value 2 private String randomValue is returned; 3 @ RequestMapping (value = "/randomValue", method = RequestMethod. GET) 4 public String getRandomValue () {5 return randomValue; 6}

8. directly inject files for operations

Testfile/testEl.txt

1 Qiao Feng-jianglong 18 OPS 2 Yang Guo-suddenly lost the soul
 1     @Value("classpath:testfile/testEl.txt") 2     private Resource txtResource; 3     @RequestMapping(value = "/resource", method = RequestMethod.GET) 4     public String getResource() { 5         try { 6             return IOUtils.toString(txtResource.getInputStream(), "UTF-8"); 7         } catch (IOException e) { 8             ExceptionUtils.getStackTrace(e); 9         }10         return StringUtils.EMPTY;11     }

Note: This is very important. You can use Apache. commons. io. IOUtils to operate the operating file.

9. Read the return value of the function of another bean.

1 @ Component ("eLComponent") 2 public class ELComponent {3 public String getNameBean () {4 return "Qiao Feng"; 5} 6}
1 @ Value ("# {eLComponent. getNameBean ()} ") // # Must be used here. $ is not applicable to 2 private String nameBean; 3 @ RequestMapping (value ="/nameBean ", method = RequestMethod. GET) 4 public String getNameBean () {5 return nameBean; 6}

Note: it is best to specify beanName for the called bean.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.