bonjour
je suis entrain réaliser une application spring mvc j ' ai un problème avec le contrôleur . quand j appuie sur list customer il me dirige pas sur bon url le voici (http://localhost:8080/StringTest/%3C...rlist'/%3E) de même avec edit customer
merci
voici l'home page
voici le contrôleur CustomerEditController
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 <%@ 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"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <script type="text/javascript"></script> <style type="text/css" > span{font-family: sans-serif; font-size: 14px; text-align: center; color: red; } </style> </head> <body> <form action="CustomerListController" method="post"> <div id="header"> </div> <table width="100%"> <tr height="600"> <td width="20%" valign="top"> <ul> <li><a href="<c:url value='/customerlist'/>">List Customer</a></li> <li><a href="<c:url value='/customeredit?id=${customer.id}'/>">Edit Customer</a></li> <li><a href="#">Save Customer</a></li> </ul> </td> <tr> </table> <div id="footer"> </div> </form> </body> </html>
voici customeredit.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
30
31
32
33
34
35
36
37
38
39
40
41
42 @Controller @RequestMapping("/customerlist") public class CustomerEditController { @Autowired CustomerRepository customerRepository; private Customer customer; private ModelAndView prepareModelAndView(Customer customerlist ) { ModelAndView mv = new ModelAndView("customeredit"); mv.addObject("id", customerlist.getId()); mv.addObject("customerlist", customerlist); return mv; } @RequestMapping("/customeradd") public ModelAndView playListCreate() { return prepareModelAndView(new Customer()); } @RequestMapping("/customeredit") public ModelAndView playListEdit (@RequestParam("id")long id) { Customer customerlist =(Customer) customerRepository.findById(id); return prepareModelAndView(customerlist); } @RequestMapping(value="/customeronsubmit",method=RequestMethod.POST) public ModelAndView CustomerAddSubmit( @Valid @ModelAttribute Customer customer, BindingResult bindingResult){ if (bindingResult.hasErrors()){ return new ModelAndView ("customeredit", "customer", customer); } if(customer.getId()!=null) customerRepository.udpate(customer); else customerRepository.persist(customer); return new ModelAndView("redirect:/custormer?id="+customer.getId()); } }
voici customerlist.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 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> </head> <body> <h1>Edit Customer</h1> <form:form modelAttribute="customer" method="post" action='<%=response.encodeUrl("/customeredit/customeronsubmit")%>'> <form:hidden path="id" /> <label>FirstName</label> <form:input path="firstName" /> <form:errors path="firstName" /><br> <label>LastName</label> <form:input path="lastName" /> <form:errors path="lastName" /><br> <input type="submit" value="<c:choose><c:when test="${customer.id==null}">Add Customer</c:when><c:otherwise>Edit Customer</c:otherwise></c:choose>"/> </form:form> <a href="./">home page</a><br/> </body> </html>
voici le contrôleur CustomerListController
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 <body> <table cellspacing="10"> <tr> <td>FirstName</td> <td>LastName</td> </tr> <c:forEach items="${customerList}" var="customer"> <tr> <td>${customer.firstName}</td> <td>${customer.lastName}</td> <td><a href="<c:url value='customer?id=${customer.id}'/>">display details</a> </tr> </c:forEach> </table> <br/> <a href="./">home page</a><br/> </body> </html>
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 @Controller public class CustomerListController { @Autowired CustomerRepository customerRepository; @RequestMapping(value="/customerlist") public ModelAndView showBookList(){ ModelAndView mv = new ModelAndView("customerlist"); // JSP List<Customer> list = customerRepository.findAll(); mv.addObject("customerList", list); // attached to the request. return mv; } }
Partager