Spring MVC @RequestMapping在类级别和方法级别404状态 [英] Spring mvc @RequestMapping on class level and method level 404 Status

查看:252
本文介绍了Spring MVC @RequestMapping在类级别和方法级别404状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有很多问题相同的帖子,但是似乎没有一个对我有帮助,所以这可能是重复的.

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.

我使用Maven创建spring mvc应用程序,我只有一个控制器和一种方法.当我仅在类级别上放置请求映射注释时,应用程序运行良好,但是当我将其放在类级别和方法级别上时,我发送如下请求:

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

localhost:8080/myapplication/planification/projet

我收到404错误:HTTP状态404-/myapplication/planification/WEB-INF/pages/test.jsp

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

这是我的控制器

@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

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>

推荐答案

请稍等,您正在RequestMapping值(表示从根开始)之前使用/.您应该这样删除它

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

 @RequestMapping("projet")

然后转到localhost:8080/myapplication/planification/projet

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

WEB-INF应该在/前面!

WEB-INF should have / in front!

这篇关于Spring MVC @RequestMapping在类级别和方法级别404状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆