我的代码在github
https://github.com/shiblybcc/blog-aggregator
我刚刚创建了一个 spring 框架项目。这是我的 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"
version="3.0">
<display-name>Course Rating</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
运行我的jetty服务器后,它工作完美并显示index.jsp文件的内容。但是如果我在 web.xml 文件中添加以下代码
<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>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.json</url-pattern>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
我收到错误
HTTP ERROR: 503
Problem accessing /. Reason:
Service Unavailable
我正在遵循教程,他们也在做同样的事情。我不明白我的代码有什么问题。提前致谢。
最佳答案
好的,明白了,当您调用 localhost:8080/index.htm 时发生了什么,servlet 名称“dispatcher”被执行,并且由于不存在index.html 的映射,您将无法看到index.html 页面。
要使其发挥作用,您必须付出更多的努力。
1).将您的index.html放在WEB-INF文件夹下。
2).创建 Controller :
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/index")
public class IndexPageController{
@RequestMapping(method = RequestMethod.GET)
public String showIndexPage(ModelMap model) {
return "index";
}
}
3).创建 mvc-dispatcher-servlet.xml 如下并将其放在 WEB-INF 文件夹下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.test.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".html" />
</bean>
</beans>
4).将您的 web.xml 编辑为:
<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>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
关于java - HTTP 错误 503 : can not access/. 原因 服务不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29985320/