开发者

Spring mvc form pre-filled : list with preferences

I have data in database for user's languages preferences. I want to manage thoses informations in th开发者_高级运维e 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?


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}"/>


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:

  • G.3 The checkboxes tag
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜