Spring mvc 表单预填充:包含首选项的列表

发布于 2024-10-30 16:25:35 字数 1101 浏览 0 评论 0原文

我的数据库中有用户语言偏好的数据。我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随遇而安 2024-11-06 16:25:35

您需要添加可用语言的属性并设置要用于表单的 d 的语言列表。

DTO:

public class PreferencesDto implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<Language> languages = new ArrayList<Language>(0);

    public List<Language> getLanguages() {return languages;}
    public void setLanguages(List<Language> languages) {this.languages = languages;}
}

控制器:

@RequestMapping
public String preferences(@ModelAttribute PreferencesDto preferencesDto, BindingResult result, ModelMap modelMap, HttpServletRequest request) {        
    modelMap.addAttribute("preferencesDto", preferencesDto);
        // Manage available data
        modelMap.addAttribute("languagesAvailable", languageService.getEuropeanLanguage());        

        // pre-filled DTO with prefered languages
        preferencesDto.setLanguages(getPreferedLanguages(request));

    return "preferences";
}

/** Manage preferences languages for user logged */
private List<Language> getPreferedLanguages(HttpServletRequest request) {
        final List<Language> languagesSelected = new ArrayList<Language>(0);
        // TODO : call a service to get the prefered language for the logged user and fill the list
        return languagesSelected;
}

JSP:

<form:form commandName="preferencesDto">
    <c:forEach items="${languagesAvailable}" var="item">
        <form:checkbox path="languages" value="${item}"/>${item.code6391}
    </c:forEach>
    <input type="submit" value="Save"/>
</form:form>

或使用标签:

<form:checkboxes path="languages" items="${languagesAvailable}"/>

You need to add attribute for available languages and set the languages list of the dto use for the form.

DTO:

public class PreferencesDto implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<Language> languages = new ArrayList<Language>(0);

    public List<Language> getLanguages() {return languages;}
    public void setLanguages(List<Language> languages) {this.languages = languages;}
}

Controller:

@RequestMapping
public String preferences(@ModelAttribute PreferencesDto preferencesDto, BindingResult result, ModelMap modelMap, HttpServletRequest request) {        
    modelMap.addAttribute("preferencesDto", preferencesDto);
        // Manage available data
        modelMap.addAttribute("languagesAvailable", languageService.getEuropeanLanguage());        

        // pre-filled DTO with prefered languages
        preferencesDto.setLanguages(getPreferedLanguages(request));

    return "preferences";
}

/** Manage preferences languages for user logged */
private List<Language> getPreferedLanguages(HttpServletRequest request) {
        final List<Language> languagesSelected = new ArrayList<Language>(0);
        // TODO : call a service to get the prefered language for the logged user and fill the list
        return languagesSelected;
}

JSP:

<form:form commandName="preferencesDto">
    <c:forEach items="${languagesAvailable}" var="item">
        <form:checkbox path="languages" value="${item}"/>${item.code6391}
    </c:forEach>
    <input type="submit" value="Save"/>
</form:form>

Or use tag:

<form:checkboxes path="languages" items="${languagesAvailable}"/>
握住我的手 2024-11-06 16:25:35

最简单的方法是使用 标记(尽管从您对问题的描述来看,不清楚 pathitems 应该是什么>):

<form:form commandName="preferencesDto"> 
     <form:checkboxes path = "languages" items = "${languages} />
</form:form>

另请参阅:

The easiest way is to use <form:checkboxes> tag (though from your description of the problem it's not clear what should be path and items):

<form:form commandName="preferencesDto"> 
     <form:checkboxes path = "languages" items = "${languages} />
</form:form>

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文