Accessing Application Properties in Spring-MVC

前端 未结 2 1633
情话喂你
情话喂你 2021-02-09 07:03

New to Spring-MVC.

I want to store two properties (uploadFolder=.., downloadFolder=..) in a .properties file and access it in HomeController class (automatically created

2条回答
  •  無奈伤痛
    2021-02-09 07:30

    1) Better way is by using src/main/resources

    2) yes, but this messageshave different purpose

    3)

    One way to make a bean from the properties file in src/main/resources:

    
       
          /WEB-INF/classes/folders.properties
       
    
    

    And in your controller simply inject the reference.

    Here how your controller will look like, the xml:

    
     ...
     
    
    

    The controller class, related part:

    public class MyController extends ... {
    
     private Properties foldersConfig;
    
     public void setFoldersConfig(Properties config) {
       this.foldersConfig = config;
     }
    }
    

    4) You can access if you put the properties in the View - Model, but this not good solution. Take what you need in the controller (the paths) and put it in the result.

提交回复
热议问题