Maison  >  Questions et réponses  >  le corps du texte

java - Spring MVC无法识别Controller导致返回的结果是404?

运行环境

java8 + idea2016 + spring mvc4.3 + maven3.3 + tomcat8 + ubuntu

报错信息

访问localhost:8080/HelloWeb/hello或者localhost:8080/hello都会返回404的状态码:

具体配置

项目结构

Web.xml

<web-app id="WebApp_ID" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Form Handling</display-name>

    <servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

HelloWeb-servlet.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="org.neo.sml"/>

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

</beans>

HelloController

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping(value="/hello")
    public String printHello(ModelMap model) {
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "hello";
    }
}

hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

补充

我猜测原因是spring mvc没有识别@Controller注解,但是我在HelloWeb-servlet.xml中已经声明了<context:component-scan base-package="org.neo.sml"/>,请问发生错误的原因可能是什么?

按照@傅易君的提示,参考so上面,我加入了<mvc:annotation-driven />,但是还是不行:

<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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="org.ziwenxie.sml"/>
    <mvc:annotation-driven/>

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

</beans>
阿神阿神2742 Il y a quelques jours476

répondre à tous(6)je répondrai

  • 巴扎黑

    巴扎黑2017-04-18 10:57:44

    Mettez HelloWeb-servlet.xml dans les ressources et remplacez web.xml par
    <servlet>

             <servlet-name>DispatcherServlet</servlet-name>
             <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             <init-param>
                     <param-name>contextConfigLocation</param-name>
                     <param-value>classpath:HelloWeb-servlet.xml</param-value>
             </init-param>
     </servlet>

    La configuration HelloWeb-servlet.xml déclare <context:component-scan base-package="org.neo.sml"/>
    Le chemin d'accès est http://localhost:8080/sml/hello C'est tout

    répondre
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:57:44

    Éteignez le contrôleur et voyez s'il n'est pas dans le contrôleur ?

    répondre
    0
  • PHP中文网

    PHP中文网2017-04-18 10:57:44

    Essayez localhost:8080/sml

    répondre
    0
  • 黄舟

    黄舟2017-04-18 10:57:44

    Activez d'abord la prise en charge des annotations :

    <mvc:annotation-driven />

    répondre
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:57:44

    Il y a principalement les aspects suivants auxquels il faut prêter attention :
    1 S'il faut configurer l'emplacement du fichier xml dans le fichier web.xml pour garantir que HelloWeb. -servlet.xml est dans le répertoire src, la configuration est la suivante :

    <servlet>
          <servlet-name>springmvc</servlet-name>      
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:HelloWeb-servlet.xml</param-value>
          </init-param>  
      </servlet>

    2. S'il faut le configurer dans le fichier HelloWeb-servlet.xml, comme indiqué ci-dessous :

    <mvc:annotation-driven></mvc:annotation-driven>        
        <context:component-scan base-package="cn"></context:component-scan>                
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsps/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>

    3. Le répertoire dans lequel le projet est démarré est-il correct et si le nom du projet doit être ajouté
    4. le répertoire est-il correct

    répondre
    0
  • ringa_lee

    ringa_lee2017-04-18 10:57:44

    L'accès à localhost:8080/HelloWeb/hello ou localhost:8080/hello renverra un code d'état 404

    Je pense que vous n'avez pas perdu ce chemin, n'est-ce pas ? Pourquoi HelloWeb au lieu de sml ? N'est-ce pas généralement le nom du projet ?
    Ou, si vous trouvez tomcat的server.xml, recherchez la configuration suivante.

    <Context docBase="test" path="/test" reloadable="true" source="org.eclipse.jst.jee.server:test"/>

    répondre
    0
  • Annulerrépondre