【问题标题】:Deployed Simple HelloWorld with Spring MVC app (without web.xml) gives 404 error使用 Spring MVC 应用程序(没有 web.xml)部署的简单 HelloWorld 给出 404 错误
【发布时间】:2016-06-22 06:19:53
【问题描述】:

我是带有注释的 Spring MVC 的新手,我之前使用过 Spring MVC XML 配置。当我尝试点击 url http://localhost:8080/HelloWorldApp 时出现 404 错误。我写了三个类: 1. 应用初始化器 2. 应用配置 3. 应用控制器 以下代码:

 package com.demo;
        public class AppIntializer implements WebApplicationInitializer {
            private static final String CONFIG_LOCATION = "com.demo.config";

            @Override
             public void onStartup(ServletContext servletContext) throws ServletException {

            System.out.println("Initializing Application for " + servletContext.getServerInfo());

            // Create ApplicationContext
            AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
            applicationContext.setConfigLocation(CONFIG_LOCATION);

            // Add the servlet mapping manually and make it initialize automatically
            DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
            ServletRegistration.Dynamic servlet = servletContext.addServlet("mvc-dispatcher", dispatcherServlet);

            servlet.addMapping("/");
            servlet.setAsyncSupported(true);
            servlet.setLoadOnStartup(1);
        }
    }

    package com.demo.config;
        @Configuration
        @EnableWebMvc
        @ComponentScan("com.demo")
        public class AppConfig extends WebMvcConfigurerAdapter {

        }

    package com.demo.web.controller;
    @Controller
    public class AppController {
        @ResponseBody
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String helloWorld() {
            return "Hello World: Spring MVC without XML configuration";
        }
    }

pom.xml

<modelVersion>4.0.0</modelVersion>
    <groupId>HelloWorldApp</groupId>
    <artifactId>HelloWorldApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <spring-framework.version>4.2.1.RELEASE</spring-framework.version>
        <servlet.version>3.0.1</servlet.version>

    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>                       
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

我尝试过运行它:

mvn clean install
mvn tomcat7:run

控制台输出:

 C:\Users\workspace_new\HelloWorldApp>mvn tomcat7:run
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building HelloWorldApp 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ HelloWorldApp >>>
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloWorldApp ---
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory C:\Users\harleen.dhingra\workspace_new\HelloWorldApp\src\main\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ HelloWorldApp ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ HelloWorldApp <<<
    [INFO]
    [INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ HelloWorldApp ---
    [INFO] Running war on http://localhost:8080/
    [INFO] Using existing Tomcat server configuration at C:\Users\harleen.dhingra\workspace_new\HelloWorldApp\target\tomcat
    [INFO] create webapp with contextPath:
    Jun 22, 2016 11:11:37 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-bio-8080"]
    Jun 22, 2016 11:11:37 AM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service Tomcat
    Jun 22, 2016 11:11:37 AM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
    Jun 22, 2016 11:11:39 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-bio-8080"]

我在控制台上没有收到错误,当我点击 http://localhost:8080/HelloWorldApp/http://localhost:8080/ 时,我应该得到输出为“Hello World:没有 XML 配置的 Spring MVC”而不是 404 错误

【问题讨论】:

  • 我认为您错过了 public class AppController 上的 @Controller 注释
  • 我在 AppController 上添加了@Controller,但仍然出现 404 错误。
  • 你收到Spring-Boot的消息了吗?我建议使用它,因为它可以在没有配置文件的情况下构建完整的 Spring 应用程序。
  • @Patrick 我已经尝试过使用 Spring-Boot 并且它有效。谢谢!!!!

标签: java maven spring-mvc servlet-3.0 spring-4


【解决方案1】:

Controller 类中缺少 @Controller 注释。
你的控制器类应该像

package com.demo.web.controller;
@Controller
public class AppController {
    @ResponseBody
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String helloWorld() {
        return "Hello World: Spring MVC without XML configuration";
    }
}

编辑
此答案有两个更新
解决方案 1
将此代码添加到您的 AppInitializer 类中

applicationContext.setServletContext(servletContext);

如果解决方案不起作用,请像这样更改您的代码:
解决方案 2 我的做法略有不同,但效果很好。
以下是更改:

  1. 将您的 AppInitializer 类移动到此包(配置文件夹)并将其更改为 com.demo.config
  2. 使用 .register 方法,而不是 .setConfigLocation,如下所示
package com.demo.config;
    public class AppIntializer implements WebApplicationInitializer {

        @Override
         public void onStartup(ServletContext servletContext) throws ServletException {

        System.out.println("Initializing Application for " + servletContext.getServerInfo());

        // Create ApplicationContext
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();

        applicationContext.register(AppConfig.class);
        applicationContext.setServletContext(servletContext);

        // Add the servlet mapping manually and make it initialize automatically
        DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
        ServletRegistration.Dynamic servlet = servletContext.addServlet("mvc-dispatcher", dispatcherServlet);

        servlet.addMapping("/");
        servlet.setAsyncSupported(true);
        servlet.setLoadOnStartup(1);
    }
}

【讨论】:

  • 哎呀我错过了,我在 AppController 上添加了@Controller,但仍然出现 404 错误。还有什么可以看的。
  • 你清理过项目吗? @Harleen
  • 我再次运行了两个命令 mvn clean install & mvn tomcat7:run from cmd 但是我在浏览器中点击 localhost:8080/HelloWorldApp 后得到相同的错误 404。
  • 我在这里做了同样的测试,它奏效了。我运行命令“mvn clean install”。我只是把这个war文件放到tomcat webapps文件夹中,然后点击tomcat bin目录中的start.bat。 & 什么是 applicationContext.setConfigLocation(CONFIG_LOCATION) 中的 Config_Location; @Harleen
  • 感谢您的设置,在您的帮助下首先尝试在 AWS 上运行
猜你喜欢
  • 2017-08-27
  • 2021-02-08
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
  • 1970-01-01
  • 2019-07-08
相关资源
最近更新 更多