【发布时间】:2017-03-13 01:36:46
【问题描述】:
我见过很多类似的帖子,但没有一个能解决我的“问题?”。请告诉我我错过了什么?当我尝试转到http://localhost:8080/helloworld.html 时,我得到“HTTP 状态 404”。
HelloWorldController.java
@Controller
public class HelloWorldController {
@RequestMapping(value="/helloworld.html")
public ModelAndView helloWorld() {
return new ModelAndView("HelloWorldPage"); }
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
</web-app>
调度程序-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.site.controller"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
文件结构: https://s18.postimg.org/4j699vn6x/stack.png
pom.xml
...
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
</dependencies>
...
【问题讨论】:
-
您使用的是哪个服务器?
-
我在 Tomcat、JBoss 测试了这段代码,但总是遇到同样的错误。
-
@RequestMapping(value="/helloworld.html")没有问题。/WEB-INF/pages/下面有HelloWorldPage.jsp吗? -
是的,问题出在服务器配置上,它不加载库。
标签: spring jsp spring-mvc http-status-code-404