Bonjour à tous
j' arrive pas à insérer dans la table product de donnée car j' ai une relation manyto one entre product et category
message d'erreur Etat HTTP 400 -:La requête envoyée par le client était syntaxiquement incorrecte.
class category
class product
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 @Entity public class Category implements Serializable { /** * */ private static final long serialVersionUID = 1L; @Id @SequenceGenerator(name = "cat_seq", sequenceName = "category_sequence") @GeneratedValue(strategy = GenerationType.AUTO, generator = "cat_seq") private Long id; @Column(length = 60) private String name; @Column(length = 250) private String description; @Column(length = 60) private String filename; public Category(String name, String description, String filename) { super(); this.name = name; this.description = description; this.filename = filename; } public Category() { super(); // TODO Auto-generated constructor stub }
editproduct.jsp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 @Entity public class Product implements Serializable { /** */ private static final long serialVersionUID = 1L; @Id @SequenceGenerator(name = "prod_seq", sequenceName = "product_sequence") @GeneratedValue(strategy=GenerationType.AUTO,generator ="prod_seq") private Long id ; @Column(length = 60) private String name; @ManyToOne @JoinColumn(name="id_category",referencedColumnName="id") private Category category; public Product() { super(); // TODO Auto-generated constructor stub } protected Product(String name, Category category) { super(); this.name = name; this.category = category; }
controller
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 <form:form modelAttribute="product" method="post" action='<%=response.encodeURL("productSubmit")%>'> <form:hidden path="id"/> <fieldset> <table > <tr> <td> Name:</td> <td><form:input path="name" /></td> </tr> <tr> <td> Category:</td> <td><form:select path="category.id" > <form:option value="" label="Please Select" /> <form:options items="${categoriesList}" itemValue="id" itemLabel="name"/> </form:select></td> </tr> <tr> <td class="btn btn-primary"><input type="submit" value="<c:choose><c:when test="${product.id==null}">Add </c:when><c:otherwise>Edit</c:otherwise></c:choose>"/></td> </tr> </table> </fieldset> <a href="<c:url value='/'/>">home page</a><br/> </form:form>
merci pour votre aide
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 @Controller @RequestMapping(value="/product") public class ProductController { @Autowired private ProductRepository productRepository; @Autowired private CategoryRepository categoryRepository; private ModelAndView prepareModelAndView(Product product ) { ModelAndView mv = new ModelAndView("editproduct"); mv.addObject("id",product.getId()); mv.addObject("product",product); List<Product> listproduct = productRepository.findAll(); //liste product List<Category> listcategory = categoryRepository.findAll(); //liste category mv.addObject("productsList", listproduct); mv.addObject("categoriesList", listcategory); //afficher liste deroulant de category return mv; } @RequestMapping("/create") public ModelAndView bookCreate(){ return prepareModelAndView(new Product()); } @RequestMapping ("/delete") public ModelAndView removeBook(@RequestParam("id")Long id){ productRepository.delete(id); return new ModelAndView ("redirect:/product/productlist"); } @RequestMapping("/productSubmit") public ModelAndView addEProduct(@Valid @ModelAttribute Product product, @RequestParam("idcategory") Long idcategory, BindingResult result){ if (result.hasErrors()) { return new ModelAndView("editproduct", "product", product); } product.getCategory().setId(idcategory); if (product.getId() != null) { productRepository.merge(product); } else { productRepository.persist(product); } return new ModelAndView ("redirect:/product/productlist"); } @RequestMapping("/edit") public ModelAndView bookEdit(@RequestParam("id") Long id){ Product product =productRepository.findById(id); return prepareModelAndView(product); } }
Partager