当我使用 url 时http://localhost:8080/springdemo/hello
..它显示 404 not found 错误..我已经像往常一样在 Maven 项目中将我的 java 文件放在 src/main/java 中。我的控制器代码如下:-
package org.abhishek;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.lang.System;
@Controller
public class HelloWorldController {
@RequestMapping(value="/hello", method = RequestMethod.GET)
public ModelAndView helloWorld() {
System.out.println("hello**");
String message = "Hello World, Spring MVC @ Javatpoint";
return new ModelAndView("hello", "message", message);
}
}
下面给出的 Web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="java.sun.com/xml/ns/javaee"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="java.sun.com/xml/ns/javaee java.sun.com/xml/ns/javaee/web-app_2_5.xsd">;
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
我把这一行System.out.println()
用于调试目的,我发现这个方法不是用上面提到的url执行的......即http://localhost:8080/springdemo/hello
。请提前回答..thanx。