Spring中<form:select>的多对多绑定问题</form:select>

时间:2014-10-21 18:55:14

标签: java jsp spring-mvc many-to-many

当我加载页面以创建新文章时,所有类别都会显示在选择框中。

当我填写新文章的表单并单击提交按钮时,它会将所有数据从表单保存到数据库,但类别不会与文章结合。

你了解我的想法吗?可能是什么问题?

ArticleController.class

@Controller
public class ArticleController {

@Autowired
ArticleService articleService;

@Autowired
CategoryService categoryService;

@Autowired
UserService userService;

@ModelAttribute("article")
public Article construct() {
    return new Article();
}

/**
 * Method displays page with all articles, categories and users
 * (articles.jsp)
 * */
@RequestMapping("/admin/articles")
public String articles(Model model) {
    model.addAttribute("articles", articleService.findAll());
    model.addAttribute("categories", categoryService.findAll());
    model.addAttribute("users", userService.findAll());
    return "articles";
}

/**
 * Method for display article's detail (article-detail.jsp)
 * */
@RequestMapping("/admin/articles/{id}")
public String userDetail(Model model, @PathVariable Integer id) {
    model.addAttribute("articles", articleService.findAll());
    model.addAttribute("article", articleService.findById(id));
    return "article-detail";
}

/**
 * Method for display article's add page (article-add.jsp)
 * */
@RequestMapping("/admin/articles/new")
public String articleAdd(Model model) {
    model.addAttribute("articles", articleService.findAll());
    model.addAttribute("categories", categoryService.findAll());
    model.addAttribute("users", userService.findAll());
    return "article-add";
}

/**
 * Method for save article
 * */
@RequestMapping(value = "/admin/articles/new", method = RequestMethod.POST)
public String saveArticle(@ModelAttribute("article") Article article,
        BindingResult result) {
    Date publishDate = new Date();
    article.setPublishDate(publishDate);
    articleService.save(article);
    return "redirect:/admin/articles.html?success=true";
}

/**
 * Binder for required date format
 * */
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
    dateFormat.setLenient(false);
    webDataBinder.registerCustomEditor(Date.class, "publishDate",
            new CustomDateEditor(dateFormat, false));
}}

物品add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%@ include file="../layouts/taglibs.jsp"%>

<form:form commandName="article" cssClass="form-horizontal">
<div class="form-group">
    <form:hidden path="id" class="form-control input-sm" />
</div>
<div class="form-group">
    <label for="title" class="col-sm-2 control-label">Titulek</label>
    <div class="col-sm-10">
        <form:input path="title" cssClass="form-control" />
    </div>
</div>
<div class="form-group">
    <label for="content" class="col-sm-2 control-label">Text</label>
    <div class="col-sm-10">
        <form:textarea path="content" cssClass="form-control" rows="10" />
    </div>
</div>
<div class="form-group">
    <label for="categories" class="col-sm-2 control-label">Kategorie</label>
    <div class="col-sm-10">
        <form:select path="categories" items="${categories}" itemLabel="name" itemValue="id"      multiple="true" cssClass="form-control" />
    </div>
</div>
<div class="form-group">
    <label for="user" class="col-sm-2 control-label">Uživatel</label>
    <div class="col-sm-10">
        <form:select path="user.id" items="${users}" value="${user.id}" itemLabel="name" itemValue="id"  cssClass="form-control" />
    </div>
</div>

<div class="form-group">
    <div class="col-sm-2">
        <input type="submit" value="Uložit" class="btn btn-primary">
    </div>
</div>

Category.class

@Entity
public class Category {

@Id
@GeneratedValue
private Integer id;

@Size(min=3, max=20, message="Název musí obsahovat 3-20 znaků!")
private String name;

@ManyToMany(mappedBy = "categories", cascade = CascadeType.REMOVE)
private List<Article> articles;

/**
 * Getters and setters
 * */
}

1 个答案:

答案 0 :(得分:1)

- 已解决 -

我通过添加:

解决了这个问题
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws     Exception {
binder.registerCustomEditor(Category.class, "categories", new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
    Category c = categoryService.findById(Integer.parseInt(text));
    setValue(c);
}
});

}