gpt4 book ai didi

java -
in Spring 的多对多绑定(bind)问题

转载 作者:行者123 更新时间:2023-12-01 12:23:51 25 4
gpt4 key购买 nike

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

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

你明白我的想法吗?可能是什么问题?

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));
}}

article-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>

类别.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
* */
}

最佳答案

-- 已解决 --

我通过添加解决了这个问题:

@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);
}
});

}

关于java - <form :select> in Spring 的多对多绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26494180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com