gpt4 book ai didi

java - 使用 Spring MVC 应用程序(没有 web.xml)部署的简单 HelloWorld 给出 404 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:53:25 26 4
gpt4 key购买 nike

enter image description here

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

 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: Spring MVC without XML configuration”的输出,而不是 404 错误

最佳答案

Controller 类中缺少 @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);

如果解决方案不起作用,请像这样更改您的代码:
方案二我以略有不同的方式完成了它并且它起作用了。
以下是更改:

  1. move your AppInitializer class to this package(config folder) & change it to com.demo.config
  2. Use .register method, instead of .setConfigLocation as shown below
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);
}
}

关于java - 使用 Spring MVC 应用程序(没有 web.xml)部署的简单 HelloWorld 给出 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37960038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com