Spring mvc @RequestMapping on class level and method level 404 Status

两盒软妹~` 提交于 2021-02-05 08:21:06

问题


I know that there are lot of posts here with the same problem, but none of them seem to help me, so this will probably be a duplicate.

Im creating a spring mvc application using Maven, i have only one controller with one method. When i put the request mapping annotation only on the class level the application works fine but when i put it on the class level and the method level and i send a request like this:

localhost:8080/myapplication/planification/projet

i get 404 error: HTTP Status 404 - /myapplication/planification/WEB-INF/pages/test.jsp

here is my controller

@Controller
@RequestMapping("/planification")
public class PlanificationController {

 @RequestMapping("/projet")
 public ModelAndView projets (ModelAndView m){

    m.addObject("projets", "All projects");
    m.setViewName("test");

    return m;
 }

}

mvc-dispatcher-servlet.xml

<beans>

 <context:component-scan base-package="com.smit"/>

 <mvc:annotation-driven/>

 <!-- **** VIEW RESOLVER BEAN **** -->

 <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

  <property name="prefix">
        <value>WEB-INF/pages/</value>
  </property>

  <property name="suffix">
        <value>.jsp</value>
  </property>

 </bean>

</beans>

web.xml

<web-app>
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-    class>
</servlet>

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

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
         /WEB-INF/mvc-dispatcher-servlet.xml

    </param-value>
</context-param>
</web-app>

回答1:


Hold on, you are using / in front of RequestMapping value, which means from the root. You should removed it like this

 @RequestMapping("projet")

Then go to localhost:8080/myapplication/planification/projet

Edit:

WEB-INF should have / in front!



来源:https://stackoverflow.com/questions/37578558/spring-mvc-requestmapping-on-class-level-and-method-level-404-status

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