java - Spring MVC无法识别Controller导致返回的结果是404?
阿神
阿神 2017-04-18 10:55:44
0
6
464

运行环境

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

报错信息

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

具体配置

项目结构

Web.xml

 Spring MVC Form Handling  HelloWeb  org.springframework.web.servlet.DispatcherServlet  1   HelloWeb /  

HelloWeb-servlet.xml

      

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上面,我加入了,但是还是不行:

       
阿神
阿神

闭关修行中......

reply all (6)
巴扎黑

Put HelloWeb-servlet.xml into resources and change web.xml to

DispatcherServlet org.springframework.web.servlet.DispatcherServlet  contextConfigLocation classpath:HelloWeb-servlet.xml  

The configuration HelloWeb-servlet.xml declares
The access path is just http://localhost:8080/sml/hello

    伊谢尔伦

    Power off the controller and see if the controller is not connected?

      洪涛

      Try localhost:8080/sml

        黄舟

        Enable annotation support first:

          伊谢尔伦

          There are mainly the following aspects to pay attention to:
          1. Whether to configure the location of the xml file in the web.

           springmvc org.springframework.web.servlet.DispatcherServlet  contextConfigLocation classpath:HelloWeb-servlet.xml  

          2. Is it configured in the HelloWeb-servlet.xml file, as shown below:

               

          3. Is the directory in which the project is started correct? Do you need to add the project name?

          4. After startup, is the directory correct?

            左手右手慢动作

            Accessing localhost:8080/HelloWeb/hello or localhost:8080/hello will return a 404 status code

            I think you didn’t lose this path, right? Why isHelloWeb而不是sml?一般不是项目的名称么,默认是。
            或者,你找到tomcat的server.xml? Find the following configuration.

              Latest Downloads
              More>
              Web Effects
              Website Source Code
              Website Materials
              Front End Template
              About us Disclaimer Sitemap
              php.cn:Public welfare online PHP training,Help PHP learners grow quickly!