user1939358
user1939358

Reputation:

RequestMapping leads to 404 Error

I'm trying to call a delete method from my jsp. It should map to the delete method in my controller. That's my code: In JSP:

<td><a href="deleteEntry/${product.name}">Delete Entry</a></td>

In Controller:

@Controller
@RequestMapping(value="/productbook")
public class ProductController {

@RequestMapping(value = "/deleteEntry/{name}")
public ModelAndView deleteEntry(@PathVariable String name){
    System.out.println("I'm HERE");
    .
            ... some code

}

I always get a 404 error when clicking my delete link. Any idea why?

Additionally, I get a warning for every time I click the delete link: e.g. WARNING: No mapping found for HTTP request with URI XY in DispatcherServlet with name 'mvc-dispatcher'

my web.xml

 <web-app>
 <display-name>Archetype Created Web Application</display-name>

 <servlet>
<servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Additionally, I noticed when I click the delete link that the filename "SpringMVC" is missing in the URL:

localhost:8080/productbook/deleteEntry/namenamename

my mvc-servlet xml:

<context:component-scan base-package="mypackage.controller.controller" />
<mvc:annotation-driven />


<bean class="mypackage.validator.GuestbookValidator" />


<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

 </beans>

Upvotes: 2

Views: 7279

Answers (3)

user1939358
user1939358

Reputation:

I found the solution, finally! Added two ** to both the mapping of the class and method. Now the error is gone, and my Sysout was called :)

@Controller
@RequestMapping(value="/productbook/**")
public class ProductController { 

@RequestMapping(value="**/deleteEntry/{name}", method = RequestMethod.GET)
public ModelAndView deleteEntry(@PathVariable String name) {
    System.out.println("I'm here!");
    SOME CODE
    SOME CODE
    SOME CODE

    return model;

}

my link:

<a href="<c:url value="/productbook/deleteEntry/${product.name}" />">Delete Entry</a>

Upvotes: 2

Pavel Horal
Pavel Horal

Reputation: 18183

Remove the dollar sign from your mapping definition:

@RequestMapping(value = "/deleteEntry/{name}")

And always generate URLs in a proper way:

<c:url var="deleteUrl" value="/productbook/deleteEntry/${product.name}" />
<td><a href="${deleteUrl}">Delete Entry</a></td>

Upvotes: 0

user2461083
user2461083

Reputation:

you need to add productbook and servlet mapping as prefix:

<td><a href="/{servlet-mapping}/productbook/delete/${product.name}">Delete Entry</a></td>

so for example - if you mapped the dispatcher servlet to "/api" (and you have a api-servlet.xml file under the WEB-INF folder) then the link should be:

<td><a href="/api/productbook/delete/${product.name}">Delete Entry</a></td>

unless you're using some kind of TLD that saves you from doing this...

Upvotes: 0

Related Questions