Results 1 to 8 of 8

Thread: @PathVariable Problem

  1. #1
    Join Date
    Apr 2009
    Location
    India
    Posts
    13

    Default @PathVariable Problem

    Hi All,

    I am making POC on spring 3.0.4. Regarding that I am getting simple prblem.
    Below is the application description.


    1)contactList.jsp

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <html>
    <head>
    <title>All Contacts</title>
    </head>
    <body>
    <h3>Contacts</h3>
    <c:if test="${!empty contactList}">
    <table class="data">
    <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Telephone</th>
    <th>&nbsp;</th>
    </tr>
    <c:forEach items="${contactList}" var="contact">
    <tr>
    <td>${contact.lastname}, ${contact.firstname} </td>
    <td>${contact.email}</td>
    <td>${contact.telephone}</td>
    <!-- <td><a href="delete.html">delete</a></td> -->
    <td><a href="delete.html/${contact.id}">delete</a></td>
    </tr>
    </c:forEach>
    </table>
    </c:if>
    </body>
    </html>

    2) ContactController.java

    package com.lnt.infotech.web.controller;



    import java.util.Map;

    import javax.validation.Valid;
    import javax.validation.Validator;



    import org.springframework.beans.factory.annotation.Autow ired;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttri bute;
    import org.springframework.web.bind.annotation.PathVariab le;
    import org.springframework.web.bind.annotation.RequestMap ping;
    import org.springframework.web.bind.annotation.RequestMet hod;
    import org.springframework.web.servlet.ModelAndView;

    import com.lnt.infotech.model.Contact;
    import com.lnt.infotech.service.ContactService;

    @Controller
    public class ContactController {

    @Autowired
    private ContactService contactService;


    @RequestMapping("/index")
    public String listContacts(Map<String, Object> map) {

    map.put("contact", new Contact());
    map.put("contactList", contactService.listContact());
    System.out.println("Method->listContacts");
    return "contact";
    }

    @RequestMapping(value = "/addContactData", method = RequestMethod.POST)
    public ModelAndView addContact(@Valid Contact contact,BindingResult result,Map<String,Contact> model) {

    contactService.addContact(contact);
    System.out.println("Method->addContact");
    if (result.hasErrors()) {
    return new ModelAndView("contact");
    }
    model.put("contact", contact);
    //return "redirect:/contactList";
    return new ModelAndView("contactList","contactList",contactSe rvice.listContact());
    }

    @RequestMapping("/contactList")
    public ModelAndView contactList(Map<String, Object> map) {

    //map.put("contact", new Contact());
    //map.put("contactList", contactService.listContact());
    System.out.println("Method->contactList.");
    return new ModelAndView("contactList","contactList",contactSe rvice.listContact());
    }

    @RequestMapping("/delete/{contactId}")
    public String deleteContact(@PathVariable int contactId,Map<String, Object> map) {

    contactService.removeContact(contactId);
    map.put("contactList", contactService.listContact());
    System.out.println("Method->deleteContact");
    return "contactList";
    }


    }


    My Problem:-> In contactList.jsp page there is a link "delete". When I press the delete link ,In controller "deleteContact" must be call with URL(http://localhost:8080/Spring3.0MVC/delete.html/4) this. 4 is my contact id which is passing through the URL. but I am getting 404 error, page not found. I am unable to send the contact Id from URL . Please help me. If u want any more information regarding application plz let me kn.
    Thanks & Regards,
    Bishwa Ranjan Sarkar

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    First don't post duplicates ....

    Next use [ code][/code ] tags when posting so that we can actually read the code !!! :mad !!!..

    You are mapping your controller to /delete and the request you issue is /delete.html... /delete != /delete.html si it isn't going to map (this is also what the stacktrace/error message is telling you).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2009
    Location
    India
    Posts
    13

    Default

    My apologies for that this is my first time so I have committed mistake.

    yeah from delete.html I am passing contact id URL(http://localhost:8080/Spring3.0MVC/delete.html/4).
    But controller unable to find the matching method.
    Last edited by bishu24june; Jul 14th, 2011 at 06:13 AM.
    Thanks & Regards,
    Bishwa Ranjan Sarkar

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    as stated you are mapping to /delete but issueing /delete.html... That obviously is never going to match!!!
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Apr 2009
    Location
    India
    Posts
    13

    Default

    Then what should I need to give.

    If my application hv this code.

    1)contactList.jsp

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <html>
    <head>
    <title>All Contacts</title>
    </head>
    <body>
    <h3>Contacts</h3>
    <c:if test="${!empty contactList}">
    <table class="data">
    <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Telephone</th>
    <th>&nbsp;</th>
    </tr>
    <c:forEach items="${contactList}" var="contact">
    <tr>
    <td>${contact.lastname}, ${contact.firstname} </td>
    <td>${contact.email}</td>
    <td>${contact.telephone}</td>
    <!-- <td><a href="delete.html">delete</a></td> -->
    <td><a href="delete.html/${contact.id}">delete</a></td>
    </tr>
    </c:forEach>
    </table>
    </c:if>
    </body>
    </html>

    2) ContactController.java

    package com.lnt.infotech.web.controller;



    import java.util.Map;

    import javax.validation.Valid;
    import javax.validation.Validator;



    import org.springframework.beans.factory.annotation.Autow ired;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttri bute;
    import org.springframework.web.bind.annotation.PathVariab le;
    import org.springframework.web.bind.annotation.RequestMap ping;
    import org.springframework.web.bind.annotation.RequestMet hod;
    import org.springframework.web.servlet.ModelAndView;

    import com.lnt.infotech.model.Contact;
    import com.lnt.infotech.service.ContactService;

    @Controller
    public class ContactController {

    @Autowired
    private ContactService contactService;


    @RequestMapping("/index")
    public String listContacts(Map<String, Object> map) {

    map.put("contact", new Contact());
    map.put("contactList", contactService.listContact());
    System.out.println("Method->listContacts");
    return "contact";
    }

    @RequestMapping(value = "/addContactData", method = RequestMethod.POST)
    public ModelAndView addContact(@Valid Contact contact,BindingResult result,Map<String,Contact> model) {

    contactService.addContact(contact);
    System.out.println("Method->addContact");
    if (result.hasErrors()) {
    return new ModelAndView("contact");
    }
    model.put("contact", contact);
    //return "redirect:/contactList";
    return new ModelAndView("contactList","contactList",contactSe rvice.listContact());
    }

    @RequestMapping("/contactList")
    public ModelAndView contactList(Map<String, Object> map) {

    //map.put("contact", new Contact());
    //map.put("contactList", contactService.listContact());
    System.out.println("Method->contactList.");
    return new ModelAndView("contactList","contactList",contactSe rvice.listContact());
    }

    @RequestMapping("/delete/{contactId}")
    public String deleteContact(@PathVariable int contactId,Map<String, Object> map) {

    contactService.removeContact(contactId);
    map.put("contactList", contactService.listContact());
    System.out.println("Method->deleteContact");
    return "contactList";
    }


    }
    Thanks & Regards,
    Bishwa Ranjan Sarkar

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Use [ ocde][/code ] Tags

    Well the controller listens to /delete NOT /delete.html... So you either change your link to /delete or let the controller listen to /delete.html... I suggest reading the reference guide and first get an understanding of how mapping works, you seem to be missing this basic and crucial information...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Apr 2009
    Location
    India
    Posts
    13

    Default

    I have used /delete instead of /delete.html I am getting same error.

    and Sorry to say,Please tell me how to use [/code ] Tags.
    Thanks & Regards,
    Bishwa Ranjan Sarkar

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Make sure your servlet mapping is to /* and make sure the form is submitted to the correct url...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •