RequestMapping leads to 404 Error

▼魔方 西西 提交于 2021-01-27 22:13:59

问题


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>

回答1:


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>



回答2:


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




回答3:


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>


来源:https://stackoverflow.com/questions/17002543/requestmapping-leads-to-404-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!