[英]File format to save data in file and How to read this data - Spring MVC
我是Spring MVC的新手。 在處理項目時,存在用例,在這種情況下,我需要從文件中獲取一些數據並將其填充到jsp中。
public HashMap<String, HashMap<String, ArrayList<String>>> getFooterMoreLinks(){
HashMap<String, HashMap<String, ArrayList<String>>> myArray = new HashMap<String, HashMap<String, ArrayList<String>>>();
HashMap<String, ArrayList<String>> valueArr = new HashMap<String, ArrayList<String>>();
valueArr.put("heading",new ArrayList<String>() {{add("More Links");} } );
valueArr.put("row1",new ArrayList<String>() {{add("About-us");add("http://www.example.com");} } );
valueArr.put("row2",new ArrayList<String>() {{add("Investor Relation");add("http://www.example.com");} } ); //so on
myArray.put("more_links", valueArr);
HashMap<String, ArrayList<String>> partnerSiteLinksArray = new HashMap<String, ArrayList<String>>();
partnerSiteLinksArray.put("heading" ,new ArrayList<String>() {{add("Partner Heading");} } );
partnerSiteLinksArray.put("row1" ,new ArrayList<String>() {{add("Support");add("http://www.example.com");} } );
partnerSiteLinksArray.put("rowe" ,new ArrayList<String>() {{add("Daily Deails");add("http://www.example.com");} } ); //so on
myArray.put("partner_programs", partnerSiteLinksArray);
}
並在jsp中myArray
。 一切正常,但我需要從文件中選擇所有這些硬編碼的值。 請幫忙! 因為.properties文件不支持類似數組的格式。
然后,您需要擁有.properties
文件,它是標准格式的java屬性文件,格式如下:
foo.properties
some.property.key=42
someother.property.key=Username has to be at least 5 characters long
有關.properties文件的格式和用法的更多信息,請參見此處 。
首先,您需要聲明spring用於訪問屬性文件的bean。
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:foo.properties</value>
<value>classpath*:bar.properties</value>
</list>
</property>
</bean>
或者,當您使用Spring Java Configuration時,必須使用
@PropertySource("classpath:/com/myco/app.properties")
然后,您無法通過Java類中的屬性對象訪問屬性文件。
@Component
class MyClass {
@Resource(name="foo")
private Properties fooProperties;
@Resource(name="bar")
private Properties barProperties;
//Use your properties as neccessary
}
或者,您可以直接從屬性文件中注入具體值。 當您擁有較少的值時,這更簡單。 在@Value批注中,使用屬性名稱(鍵)注入其值
@Value("${some.property.key}")
private String value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.