【发布时间】:2017-02-24 13:50:24
【问题描述】:
我是 SpringMVC 的新手,正在尝试执行简单的 hello world 程序。但是,当在我的浏览器 (http://localhost:8080/FirstspringMVCwithannotation/welcome) 中运行它时,我得到了 HTTP 状态 - 404 错误。这是代码:
HelloController.java
package com.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/welcome")
public ModelAndView helloWorld(){
ModelAndView model=new ModelAndView("HelloPage");
model.addObject("msg","hello world");
return model;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FirstspringMVCwithannotation</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>
spring-dispatcher-servlet.xml
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.example" />
<mvc:annotation-driven/>
<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>
HelloPage.jsp
<html>
<body>
<h1>
First Spring MVC Application Demo
</h1>
<h2>
${msg}
</h2>
</body>
</html>
这是我的项目结构,我在 lib 文件夹下添加了所有 spring jar 文件
我试图查看其他解决方案,但这并没有解决我的问题..谁能帮助我,为什么我得到 HTTP 状态 - 404 错误?提前致谢
【问题讨论】:
-
尝试将
<url-pattern>/</url-pattern>改为<url-pattern>/*</url-pattern>,参考stackoverflow.com/questions/29792677 -
@RequestMapping("/welcome")是错误的原因将其更改为/以工作或遵循上述评论 -
你的项目文件结构是什么?您的 jsp 视图位于哪个文件夹路径中?
-
@EricWang 我已经尝试了这两种方法,但都不起作用...我收到 HTTP 状态 404 - /FirstspringMVCwithannotation/WEB-INF/.jsp 这是什么意思? tomcat服务器控制台说spring servlet初始化完成..是控制器找不到HelloPage.jsp。但是,当我尝试 /welcome 控制台显示此 org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/FirstspringMVCwithannotation/welcome] in DispatcherServlet with name 'spring-dispatcher'...跨度>
-
@MosheArad 我的 jsp 文件在 WEB-INF 下
标签: java spring spring-mvc