Bonjour,
voilà je débute en Spring MVC et j'ai un petit problème.
Lorsque je clique sur le bouton qui submite ma page je vois bien qu'un POST a été envoyé mais je n'arrive pas dans la méthode onSubmit du controller.
voici mon code:
le 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class CarNewController extends SimpleFormController { @Override protected Object formBackingObject(HttpServletRequest request) throws Exception { Car defaultCar = new Car(); defaultCar.setModel("Type new model here"); defaultCar.setPrice(new BigDecimal(25000)); return defaultCar; } @Override protected Map referenceData(HttpServletRequest request) throws Exception { Map<Object, Object> dataMap = new HashMap<Object, Object>(); BrandManager brandManager = new BrandManager(); dataMap.put("brandList", brandManager.getBrandList()); return dataMap; } @Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { // binder.setDisallowedFields(new String[] { "brand" }); Car car = (Car) binder.getTarget(); // set car's brand from request parameter brand id BrandManager brandManager = new BrandManager(); Long brandId = null; try { brandId = Long.parseLong(request.getParameter("brand")); } catch (Exception e) { } if (brandId != null) { Brand brand = brandManager.getBrandById(brandId); car.setBrand(brand); } } @Override public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) { CarManager carManager = new CarManager(); carManager.createCar((Car) command); return new ModelAndView(new RedirectView(getSuccessView())); }
la jsp:
Merci de vos réponses!
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 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib uri="/tld/spring-form" prefix="form" %> <html> <body> <h1>New Car</h1> <form:form method="post"> Brand<br /> <form:select path="brand"> <form:options items="${brandList}" itemLabel="name" itemValue="id" /> </form:select> <br /><br /> Model<br /> <form:input path="model"/><br /><br /> Price<br /> <form:input path="price"/><br /><br /> <input type="submit" value="Submit"> </form:form> </body> </html>
Partager