Spring MVC没有检测到我的控制器

时间:2018-11-08 14:55:33

标签: java spring

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

2 个答案:

答案 0 :(得分:2)

嗨,您正在控制器中返回“ 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

默认conext-path此处是您的应用名称 如果您转到TOMCAT_HOME / webapps /

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

所以这就是为什么本地主机无法找到它的原因: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   

谢谢

答案 1 :(得分: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,而不是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>

通过这些更改,它可以工作。