java HTTP 错误 503:无法访问 /。原因服务不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29985320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:14:40  来源:igfitidea点击:

HTTP ERROR 503: can not access /. Reason Service Unavailable

javaspringweb.xmlurl-pattern

提问by asdfkjasdfjk

My code is in github

我的代码在github

https://github.com/shiblybcc/blog-aggregator

I have just created a spring framework project. This is my web.xml file code

我刚刚创建了一个 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>

After I run my jetty server this works perfect and show the content of index.jsp file. But if I add following code in the web.xml file

在我运行我的码头服务器之后,它完美运行并显示了 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>

I get the error

我收到错误

HTTP ERROR: 503    
Problem accessing /. Reason:    
    Service Unavailable

I am following a tutorial and they are doing the same. I don't understand what is wrong with my code. Thanks in advance.

我正在学习教程,他们也在做同样的事情。我不明白我的代码有什么问题。提前致谢。

采纳答案by asdfkjasdfjk

After whole day research I found the solution here Getting Error scanning file when running jetty 9 on java 8 using the maven jetty plugin

经过一整天的研究,我在这里找到了解决方案, 使用 maven jetty 插件在 java 8 上运行 jetty 9 时获取错误扫描文件

Also I updated jetty, maven and tomcat. My current pom.xml

我还更新了码头、Maven 和 tomcat。我当前的 pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                    <version>5.0.3</version>
                </dependency>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm-commons</artifactId>
                    <version>5.0.3</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.3</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

After updating everything, it is working fine now.

更新一切后,它现在工作正常。

回答by Arpit Aggarwal

Ok, understood, what is happening when you are calling localhost:8080/index.htm, servlet name "dispatcher" got executed and as there is no mapping for index.html exist, you will not be able to see the index.html page.

好的,明白了,当您调用 localhost:8080/index.htm 时发生了什么,servlet 名称“dispatcher”被执行,并且由于不存在 index.html 的映射,您将无法看到 index.html 页面.

To make it work, you have to put little more efforts.

为了让它发挥作用,你必须付出更多的努力。

1). Put your index.html under WEB-INF folder.

1)。将您的 index.html 放在 WEB-INF 文件夹下。

2). Create Controller:

2)。创建控制器:

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). Create mvc-dispatcher-servlet.xml as below and put it under WEB-INF folder :

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). Edit your web.xml as:

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>