Access Application Properties in Spring -MVC

New for Spring -MVC.

I want to save two properties (uploadFolder = .., downloadFolder = ..) in a .properties file and access to it in the HomeController class (automatically created by the MVC template).

Can you advise me how ..

1) Created the app.properties file with the above and placed it under / src / main / resources. is this correct or should it go to / webapp / resources?

2) Put a bean in servlet-context.xml as follows. it is right?

<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <beans:property name="basename" value="app" /> </beans:bean> 

3) How do I access this in Java controllers?

4) How do I access them in JSP?

I canโ€™t say how much I thank you.

+6
source share
2 answers

There are several ways to do this. I do the following. In the context of the application:

 <util:properties id="myProps" location="classpath:app.properties" /> 

Make sure the following is at the top of the file: "/">

 xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation= "... http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" 

I usually put my property files in src / main / resources. As long as they are on their way to class, you are good. Then in your controller, you can annotate your field / method with:

 @Controller public class MyController { @Value("#{myProps['uploadFolder']}") private String uploadFolder @Value("#{myProps['downloadFolder']}") private String downloadFolder @RequestMapping("myPage") public String loadPage(ModelMap m) { m.addAttribute("uploadFolder", uploadFolder); m.addAttribute("downloadFolder", downloadFolder); return "myPage"; } public void setUploadFolder(String uploadFolder) { this.uploadFolder = uploadFolder; } public void setDownloadFolder(String downloadFolder) { this.downloadFolder = downloadFolder; } } 

Then in your JSP:

 Download folder: ${downloadFolder} <br/> Upload folder: ${uploadFolder} 

NTN. Let me know if you have any questions.

+20
source

1) It is best to use src/main/resources

2) yes, but this messages has a different purpose

3)

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

 <bean id="foldersConfig" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" > <value>/WEB-INF/classes/folders.properties</value> </property> </bean> 

And in your controller just enter the link.

Here is what your controller looks like, xml:

 <bean id="myController" class="my.project.package.controller.MyController"> ... <property name="foldersConfig" ref="foldersConfig" /> </bean> 

Controller class related part:

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

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

0
source

Source: https://habr.com/ru/post/912608/


All Articles