Java – accessing application properties in spring MVC

New features of spring MVC

I think so Two properties are stored in the properties file (uploadfolder =.., downloadfolder =..) Access it in the homecontroller class (automatically created by the MVC template)

How can you guide me

1) Create an app using the above content Properties file and put it under / SRC / main / resources Is this correct or should it be under / webapp / resources?

2) In the servlet context.xml file in the following way Place a bean in the XML Is it correct?

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

3) Now how do I access it in the Java controller?

4) How do I access these in JSP?

I don't know how much I'll thank you

Solution

There are several different ways to do this I did the following In the app environment:

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

Make sure the following is at the top of the file to include the "util" namespace:

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 properties file in Src / main / resources As long as they are in class, you will be fine Then, in your controller, you can annotate your field / method with the following command:

@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}

HTH. If you have any questions, please let me know

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>