【问题标题】:HTTP ERROR 503: can not access /. Reason Service UnavailableHTTP 错误 503:无法访问 /。原因服务不可用
【发布时间】:2015-07-11 04:39:00
【问题描述】:

我的代码在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>

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

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

【问题讨论】:

  • 添加后能否查看:dispatcher/
  • 这也行不通
  • 你访问的是什么网址?
  • 您能否确认在应用程序部署期间没有错误(在日志/控制台中)?

标签: java spring web.xml url-pattern


【解决方案1】:

经过一整天的研究,我在这里找到了解决方案 Getting Error scanning file when running jetty 9 on java 8 using the maven jetty plugin

我还更新了 jetty、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>

更新所有内容后,它现在工作正常。

【讨论】:

    【解决方案2】:

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

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

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

    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)。如下创建 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>
    

    【讨论】:

    • 但我想使用Controller、RequestMapping等注解
    • 现在点击:localhost:8080/index
    • 你有什么异常吗?
    • 您的应用程序的上下文是什么?主要是应用程序名称。
    • 是的,我收到异常 java.lang.RuntimeException:扫描文件 IndexController.class 时出错。我可以从哪里检查我的应用程序上下文?
    猜你喜欢
    • 1970-01-01
    • 2014-02-17
    • 2014-05-02
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    相关资源
    最近更新 更多