Announcement Announcement Module
Collapse
No announcement yet.
@PathVariable Problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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.

  • #2
    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).

    Comment


    • #3
      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, 06:13 AM.

      Comment


      • #4
        as stated you are mapping to /delete but issueing /delete.html... That obviously is never going to match!!!

        Comment


        • #5
          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";
          }


          }

          Comment


          • #6
            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...

            Comment


            • #7
              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.

              Comment


              • #8
                Make sure your servlet mapping is to /* and make sure the form is submitted to the correct url...

                Comment

                Working...
                X