Spring MVC未检测到我的控制器。

3

我有一个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错误。


1
当您访问localhost:8888/hello_there时会发生什么? - Kamil W
1
如果端口错误,他会收到 ERR_CONNECTION_REFUSED 错误,而不是 404。 - Kamil W
你有hello.jsp文件吗? - Mykhailo Moskura
@MykhailoMoskura 不。 - cristid9
1
你发布的第一个文件名为/src/main/webapp/WEB-INF/appconfig-root.xml,是这样吗?如果是,请编辑标题。也许你应该在appconfig-mvc.xml中使用context:component-scan元素。 - sven.kwiotek
显示剩余8条评论
2个回答

3
你好,你的控制器返回了“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中查找,因此当您在控制器中返回字符串“hello”时:
@RequestMapping(value = "/hello_there", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }

它将在“hello”后添加后缀“.jsp”,并尝试查找“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

默认的上下文路径是您的应用程序名称。如果您转到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   

谢谢


它仍然不起作用。我创建了 hello.jsp,但它仍然没有显示任何内容。 - cristid9
不好意思,localhost:port/myAppName/helloThere 返回 HTTP 状态码 404 -。 - cristid9
请告诉我在tomcat_home/webapps目录中的应用程序名称。 - Mykhailo Moskura
名称为 mywebapp - cristid9
尝试拨打本地主机:8080/mywebapp/hello_there 实际上,为什么你在运行端口8888?你在Tomcat配置中指定了它吗?因为默认情况下它是8080。如果没有,如果你覆盖了它,请使用8888。 - Mykhailo Moskura
让我们在聊天中继续这个讨论 - Mykhailo Moskura

0

我花了超过5个小时来解决这个问题,以下的解决方法被证明是成功的。

首先,我修改了/WEB_INF/web.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
      <display-name>  Spring version </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>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

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

现在,我只有一个配置文件mvc-servlet-dispatcher.xml,而不是两个:

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

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

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

有了这些更改,它可以工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接