【问题标题】:Spring MVC is not detecting my controllersSpring MVC 没有检测到我的控制器
【发布时间】:2019-04-12 02:40:00
【问题描述】:

我有一个包含以下文件的 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 错误。

【问题讨论】:

  • 访问localhost:8888/hello_there时会发生什么?
  • @GauravRai1512 如果端口错误,他会得到ERR_CONNECTION_REFUSED错误而不是404
  • 你有 hello.jsp 文件吗?
  • @MykhailoMoskura no.
  • 对吗,您在此处发布的第一个文件名为 /src/main/webapp/WEB-INF/appconfig-root.xml ?如果是这样,请编辑标题。也许您应该在 appconfig-mvc.xml 中使用 元素。

标签: java spring


【解决方案1】:

您好,您在控制器中返回“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";
    }

它将为“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

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

谢谢

【讨论】:

  • 还是不行。我创建了hello.jsp,但它仍然没有显示任何内容。
  • 不,localhost:port/myAppName/helloThere 给出 HTTP 状态 404 -
  • 请告诉我tomcat_home/webapps目录下的应用名称
  • 名字是mywebapp
  • 尝试拨打电话 localhost:8080/mywebapp/hello_there 实际上为什么要运行端口 8888,您在 tomcat 配置中在哪里指定它,因为默认情况下它的 8080 如果不是,如果你覆盖它使用 8888
【解决方案2】:

我在这个问题上花费了超过 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,而不是 2 个配置文件:

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

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

【讨论】:

    猜你喜欢
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2019-11-11
    • 2019-09-19
    相关资源
    最近更新 更多