简体   繁体   English

Spring-将Web表单绑定到application.properties文件

[英]Spring - Binding web form to application.properties file

I have a web form that enables one to update the appliaction.properties, so that user does not have to go to the file and edit it. 我有一个Web表单,该表单使用户可以更新appliaction.properties,因此用户不必转到文件并进行编辑。 I wants to load the value from the application.properties file and display in the web form textfield. 我想从application.properties文件中加载值并显示在Web窗体文本字段中。 After the user edit the some changes, user can update the application.properties file. 在用户编辑了一些更改之后,用户可以更新application.properties文件。

Question - How can I bind my web form to application.properties file other than using @value in Spring? 问题-除了在Spring中使用@value之外,如何将Web表单绑定到application.properties文件? As the key/value pairs might be updated through web form, I believe @value is not the right choice. 由于键/值对可能会通过Web表单进行更新,因此我认为@value不是正确的选择。

The application properties are for the settings the application needs to load when it starts up, and then connects to a database for example, with the username and password specified in that file (although environment variables can also be used.) 应用程序属性用于应用程序启动时需要加载的设置,然后使用该文件中指定的用户名和密码(例如,也可以使用环境变量)连接到数据库。

You shouldn't really edit this file at run-time, with the exception of using the Refresh Scope annotation. 除了使用“刷新作用域”注释外,您实际上不应在运行时编辑此文件。 But I don't advise you do that as it's fairly hard in practice to achieve an application which supports changing configuration at run-time. 但我不建议您这样做,因为在实践中要获得支持在运行时更改配置的应用程序相当困难。

There is also Spring Cloud Config server for reloading properties at run-time and that provides a REST API although you mainly just use git with that one. 还有一个Spring Cloud Config服务器,用于在运行时重新加载属性,该服务器提供了REST API,尽管您主要只是在该服务器上使用git。

But the key is here that they are all mentioning configuration which is only something developers do. 但是关键是这里他们都提到了配置,这只是开发人员要做的。 I also don't think you want to use the filesystem for storing your state, much rather have database. 我也不认为您想使用文件系统来存储状态,而是拥有数据库。 There are many reasons to use a file system over a database I won't go into them now. 在数据库上使用文件系统有很多原因,我现在不再赘述。

I will make the assumption you are a beginner looking at your rep. 我将假设您是一位初学者,正在研究您的代表。 Best advice I can give you is to watch what everyone else is doing and learn from that first. 我能给您的最好建议是观察其他人在做什么,然后从中学到东西。 Most of the time people are all doing similar things because it is the right thing to do. 大多数时候,人们都在做类似的事情,因为这是正确的事情。 If you can't find anybody else changing properties files based on form submissions in their spring app but rather using databases you probably want to go with the database. 如果找不到其他人在他们的Spring应用程序中基于表单提交来更改属性文件,而是使用数据库,则可能要使用该数据库。

We were in a similar situation last month where client need update in configuration file in run time. 上个月,我们处于类似情况,客户端需要在运行时更新配置文件。 We created a central Spring component that loads all properties from file using @Value 我们创建了一个中央Spring组件,该组件使用@Value从文件加载所有属性。

@Component
public class ConfigurationManagerComponent{
  @Value("conf.username")
  private String userName;
  @Value("conf.email");
  private String email;
  /*
   * All your attributes
  */
  //getters and setters
  public String getUserName(){
    return this.userName;
  }
  public void setUserName(String userName){
   // first set the value to this class (component)
   this.userName=userName;
   //second save this value in the properties file
   //a specific method you have to implement
    saveproperties("conf.username",userName); 
  }
  /*
   * Do like this for all your getters and setters
  */ 
}

Then, in any time you need to get or save properties, just inject this component and use its getters and setters 然后,在任何时候您需要获取或保存属性时,只需注入此组件并使用其getter和setter

@Autowired
ConfigurationManagerComponent configComponent;
String myActualUserName=configComponent.getUserName();

So then you can get all your data in your controller from config component and set them to your spring model to fill form in webpage, and when saving after edit, you can always use setters of your config component(update class and properties file). 这样一来,您就可以从配置组件中获取控制器中的所有数据,并将其设置为spring模型以填写网页中的表单,并且在编辑后进行保存时,您始终可以使用配置组件的设置器(更新类和属性文件)。

This method is not a standard, just a solution we employed to resolve a problem, it's giving good results right now. 这种方法不是标准的,只是我们用来解决问题的一种解决方案,目前效果很好。 You can also check Apache commons configuration Here for other solutions.. Hope this will help. 您也可以在此处查看Apache公共配置以获取其他解决方案。希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 spring web 应用程序中找到 application.properties,虽然位置被提及为 classpath:/application.properties 但文件丢失? - How to find the application.properties in spring web app, while location is mentioned as classpath:/application.properties but file is missing? Spring application.properties文件中的布尔值? - boolean values in Spring application.properties file? Spring boot application.properties 文件读取 - Spring boot application.properties file reading Spring 引导无法识别 application.properties 文件 - Spring Boot not recognizing application.properties file 将 application.properties 绑定到 Spring 中的外部库中的 class - Binding application.properties to a class from external library in Spring Spring 引导项目未绑定来自 application.properties 的 @Value - Spring Boot project not binding @Value from application.properties 春天用通配符从application.properties文件读取属性 - Spring reading properties from application.properties file with wildcard 绑定 application.properties 文件时如何强制将字符串强制为字符串? - how to force as mandatory a String when binding an application.properties file? Spring Application Context未从application.properties文件获取属性 - Spring Application Context Not getting Property from application.properties file Spring 引导应用程序不使用 Application.Properties 文件 - Spring Boot Application not using Application.Properties file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM