Spring mvc 表单预填充:包含首选项的列表
我的数据库中有用户语言偏好的数据。我想使用 Spring MVC 在 Web 应用程序的前端管理这些信息。我按服务检索首选项,并且想显示一个包含用户首选项中预填充数据的公式。
我有一个列表,其中包含我通过控制器中的服务获得的所有可用语言:
@RequestMapping
public String preferences(@ModelAttribute PreferencesDto preferencesDto, ModelMap modelMap, HttpServletRequest request) {
final List<Language> languages = languageService.getEuropeanLanguage();
modelMap.addAttribute("languages", languages);
List<Language> userLanguages = preferenceService.getUserLanguages(getLoggedUser());
modelMap.addAttribute("userLanguages", userLanguages);
modelMap.addAttribute("preferencesDto", preferencesDto);
return "preferences";
}
在我的 jsp 中,我迭代语言列表以显示与语言相关的复选框:
<form:form commandName="preferencesDto">
<c:forEach items="${languages}" var="language">
<form:checkbox path="languages" value="${language}" id="cl${language.id}"/>
${language.code6391}
</c:forEach>
<input type="submit" value="Save"/>
</form:form>
我想显示带有用户语言首选项的预填充复选框。我怎样才能做到这一点?
I have data in database for user's languages preferences. I want to manage thoses informations in the front-end of a web application with Spring MVC. I retrieve preferences by service and I want to display a formular with pre-filled data from user's preferences.
I have a list with all available languages that I get by a service in my controller:
@RequestMapping
public String preferences(@ModelAttribute PreferencesDto preferencesDto, ModelMap modelMap, HttpServletRequest request) {
final List<Language> languages = languageService.getEuropeanLanguage();
modelMap.addAttribute("languages", languages);
List<Language> userLanguages = preferenceService.getUserLanguages(getLoggedUser());
modelMap.addAttribute("userLanguages", userLanguages);
modelMap.addAttribute("preferencesDto", preferencesDto);
return "preferences";
}
In my jsp, I iterate over the languages list to display checkboxes related to language:
<form:form commandName="preferencesDto">
<c:forEach items="${languages}" var="language">
<form:checkbox path="languages" value="${language}" id="cl${language.id}"/>
${language.code6391}
</c:forEach>
<input type="submit" value="Save"/>
</form:form>
I want to display pre-filled checkboxes with languages preferences by user. How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要添加可用语言的属性并设置要用于表单的 d 的语言列表。
DTO:
控制器:
JSP:
或使用标签:
You need to add attribute for available languages and set the languages list of the dto use for the form.
DTO:
Controller:
JSP:
Or use tag:
最简单的方法是使用
标记(尽管从您对问题的描述来看,不清楚path
和items
应该是什么>):另请参阅:
The easiest way is to use
<form:checkboxes>
tag (though from your description of the problem it's not clear what should bepath
anditems
):See also: