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

 v敏0敏v_405_961 发布于 2022-10-25 09:13

运行环境

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

报错信息

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

具体配置

项目结构

Web.xml

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    Spring MVC Form Handling

    
        HelloWeb
        
            org.springframework.web.servlet.DispatcherServlet
        
        1
    

    
        HelloWeb
        /
    

HelloWeb-servlet.xml

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

   

   
      
      
   

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


    Hello World


${message}

补充

我猜测原因是spring mvc没有识别@Controller注解,但是我在HelloWeb-servlet.xml中已经声明了,请问发生错误的原因可能是什么?

按照@傅易君的提示,参考so上面,我加入了,但是还是不行:

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

    
    

    
        
        
    

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

    这个路径我想你没输对吧?为什么是HelloWeb而不是sml?一般不是项目的名称么,默认是。
    或者,你找到tomcat的server.xml,找到如下配置。

    <Context docBase="test" path="/test" reloadable="true" source="org.eclipse.jst.jee.server:test"/>
    2022-11-12 01:41 回答
  • 主要有以下几个方面需要注意:
    1.在web.xml文件中是否配置xml文件的位置,保证HelloWeb-servlet.xml在src目录下,配置如下:

    <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.在HelloWeb-servlet.xml文件中是否配置,如下所示:

    <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.项目启动的目录是否正确,是否需要添加项目名称;
    4.启动之后,目录是否正确;

    2022-11-12 01:41 回答
  • 先开启注解支持:

    <mvc:annotation-driven />
    2022-11-12 01:41 回答
  • 试试 localhost:8080/sml

    2022-11-12 01:41 回答
  • controller打个断电,看看是没进controller吗?

    2022-11-12 01:41 回答
  • 把HelloWeb-servlet.xml放到resources里面,web.xml里面改为
    <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>

    配置HelloWeb-servlet.xml中声明了<context:component-scan base-package="org.neo.sml"/>
    访问路径写http://localhost:8080/sml/hello就可以了

    2022-11-12 01:41 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有