当我访问First_Spring_MVC /欢迎我得到以下错误:
org.springframework.web.servlet.PageNotFound noHandlerFound
警告:否的DispatcherServlet找到HTTP请求与URI [/ First_Spring_MVC /欢迎]名为“弹簧调度”映射
这里是我的文件夹结构:
这是我的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>First_Spring_MVC</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这里的弹簧调度-servlet.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<context:component-scan base-package="com.webapp.helloController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
这是我使用的控制器类的Java文件。
@Controller
public class HelloController {
@RequestMapping("/welcome")
public ModelAndView helloWorld(){
ModelAndView model = new ModelAndView("HelloPage");
model.addObject("welcomeMessage", "hello world");
return model;
}
}
您扫描的是错误的包。更改:
<context:component-scan base-package="com.webapp.helloController" />
至
<context:component-scan base-package="com.app.helloController" />
另外,还要确保你打正确的网址,你的情况:
http://localhost:{your_port_num}/First_Spring_MVC/welcome