Spring-MVC的新功能.
我想在.properties文件中存储两个属性(uploadFolder = ..,downloadFolder = ..)
在HomeController类中访问它(由MVC模板自动创建).
你能指导我怎么样..
1)使用上面的内容创建了一个app.properties文件,并将其放在/ src / main / resources下.这是正确的还是应该在/ webapp / resources下?
2)以下面的方式在servlet-context.xml中放置一个bean.它是否正确?
<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <beans:property name="basename" value="app" /> </beans:bean>
3)现在如何在Java控制器中访问它?
4)如何在JSP中访问这些?
我不知道我会多么感谢你.
解决方法
有几种不同的方法可以做到这一点.我做了以下事情.在app环境中:
<util:properties id="myProps" location="classpath:app.properties" />
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"
我通常把我的属性文件放在src / main / resources中.只要他们在课堂上,你就会很好.然后,在您的控制器中,您可以使用以下命令注释您的字段/方法:
@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; } }
然后,在你的JSP中:
Download folder: ${downloadFolder} <br/> Upload folder: ${uploadFolder}
HTH.如果您有任何疑问,请告诉我.