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上面,我加入了
,但是还是不行:
Put HelloWeb-servlet.xml into resources and change web.xml to
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:
2. Is it configured in the HelloWeb-servlet.xml file, as shown below:1. Whether to configure the location of the xml file in the web.
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?
I think you didn’t lose this path, right? Why is
HelloWeb
而不是sml
?一般不是项目的名称么,默认是。或者,你找到
tomcat的server.xml
? Find the following configuration.