Spring MVC 填充下拉列表的最佳方法 - 属性文件?
我想填充下拉列表,但不想继续访问数据库。 我正在考虑将我的国家/地区列表或语言列表放在属性文件中。这样我就可以读入它,然后将其分配给一个变量。然后我可以通过 ModelAndView 类型返回它。
这是一个好方法吗?我不知道如何存储静态数据。我不想将其保留在类中,因为如果需要更改,更新它会更困难。
I want to populate a drop down list and I do not want to keep going to the database.
I was thinking to have my country list or language list in a properties file. That way I could read it in, and then assign that to a variable. I could then return that through the ModelAndView type.
Is this a good approach? I am not sure how else to store static data. I do not want to keep it in a class because it will be harder to update it if there needs to be a change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
得到答案了!
如果我有一个国家或语言列表,任何静态的东西,我都可以在我的 spring 上下文文件中包含它:
等等。
我会在我的 ModelAndView 中注入国家/地区列表(或者我认为你甚至可以直接访问它,而无需将其放入 ModelAndView 中,因为它是在全局 Spring 上下文中)
,然后在我的 jsp 中渲染 ${countryList} -
<代码>
选项字段应该自动呈现。这就是要点,我现在要尝试一下!
谢谢!!
got an answer!
If I got a list of countries or languages, anything static, I can have this in my spring context file:
etc.
I would inject country list in my ModelAndView (or I think you can even access it directly without putting it in ModelAndView cuz it's in the global Spring context)
and then render ${countryList} in my jsp do -
<form:select path="country">
<form:option value="NONE" label="--- Select ---"/>
<form:options items="${countryList}" />
</form:select>
The options field should automatically render. That is the gyst of of, I am going to try this now!!
Thanks!!
您也可以将此数据存储在数据库中并使用缓存,例如 ehCache。由于它们存储在数据库中,因此可以轻松更新,并且缓存将避免每次需要此信息时都访问数据库。
You could as well store this data in the database and use a cache for example like ehCache. As they are stored in a database they could be updated easily and the cache would avoid to go to the database every time you need this information.