首页 > 解决方案 > Spring MVC 没有检测到我的控制器

问题描述

我有一个包含以下文件的 Spring MVC 项目:

/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
      <display-name>Archetype Created Web Application</display-name>
       <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/appconfig-root.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

和2个配置文件:

/src/main/webapp/WEB-INF/appconfig-root.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="appconfig-mvc.xml"/>

    <context:component-scan base-package="yc.servlets"/>

    <!--<context:property-placeholder location="classpath:application.properties"/>-->
</beans>

/src/main/webapp/WEB-INF/appconfig-mvc.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven/>

    <mvc:resources mapping="/resources/**" location="/resources/"/>

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

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

以及包含我的控制器的类:

package yc.servlets;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class TestPage {


    @RequestMapping(value = "/hello_there", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }
}

我认为这足以让 spring mvc 正常工作,但是当我localhost:8888在浏览器中输入时出现404错误。

localhost:8888/hello_there也给出404错误。

标签: javaspring

解决方案


嗨,您在控制器中返回“hello”,它会找到 hello.jsp 如果找不到它会抛出 404 http 错误

当您配置 InternalViewResolver 时:

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

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

它试图在 /WEB-INF/jsp/hello.jsp 中找到,因此当您在控制器中返回 String "hello" 时:

@RequestMapping(value = "/hello_there", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }

它将后缀“.jsp”添加到“hello”,它会尝试找到“hello.jsp”,因为找不到它会抛出 404

您可以从 @Controller 更改为 @RestController 隐式添加 @ResponseBody 注释,看看它是否返回“hello”作为响应

此外,如果这对您没有帮助,您可以更改:

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

对此:

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

如果它现在仍在工作,请尝试以下步骤:

试试这样的网址:

localhost:8080/yourApplicationName/hello_there

默认conext-path是你的应用程序名称如果你去TOMCAT_HOME/webapps/

您会发现在根文件夹中没有您的应用程序

所以这就是为什么它无法被 localhost:8080/...

您可以通过两种方式解决此问题:

1.Find your application deployed and copy to root folder in 
    TOMCAT_HOME/webapps/ROOT
 2.See the name of your application in  TOMCAT_HOME/webapps/ 
   and call url : localhost:8080/yourAppName/hello_world   

谢谢


推荐阅读