Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

spring - Spring EL(SpEL)替换属性文件中的占位符?(Spring EL (SpEL) to replace placeholders from properties file?)

I am using Spring Boot and SpEL (Expression Language) .

(我正在使用Spring Boot and SpEL (Expression Language) 。)

I am want to create placeholder in message.properties file so that value can be replace and send to UI.

(我想在message.properties文件中创建占位符 ,以便可以替换值并将其发送到UI。)

message.properties

(message.properties)

not.found={0} not found

Code:

(码:)

@Override
@Transactional
public void deleteEmployee(String employeeId) {
    int deletedCnt = employeeRepository.deleteEmployee(Integer.valueOf(employeeId));
    if(deletedCnt == 0 )
        throw new ResourceNotFoundException(env.getProperty("not.found"));
}

Here I want to show EmployeeId (Say 123) is not found.

(在这里我想显示找不到EmployeeId(说123)。)

  ask by PAA translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The value located under your not.found property is nothing more than String , so you can perform any operations on that String.

(位于not.found属性下的值not.foundString ,因此您可以对该String执行任何操作。)

Eg.

(例如。)

not.found = Employee with id %s not found

And process this message in your service however you desire.

(并根据需要在您的服务中处理此消息。)

throw new ResourceNotFoundException(String.format(env.getProperty("not.found"), employeeId));

Moreover you can access other properties and environment variables in your config.

(此外,您可以在配置中访问其他属性和环境变量。)

default.message = Exception occurred:
not.found = ${default.message} ${ENVIRONMENT_VARIABLE}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...