【发布时间】:2019-03-28 21:59:48
【问题描述】:
我有一个非常令人沮丧的问题。我有一个 web 应用程序,当然是一个 Maven 项目,它使用 Spring MVC 框架(这里只想提一下这个应用程序应该配置注释,我不想使用 XML,只是想看看它是如何工作的) .当我尝试输入 //localhost:8080/hello 时,会发生 404 错误。当我输入 //localhost:8080/hello.html 时,会查看简单而空的 html(hello.html 非常简单 - 在正文中,我刚刚读取了位于模型中的变量(由 HelloController 放置)。当然 - 结果//localhost:8080 是一个有效的索引页面 (index.html)。我使用的是 Tomcat 8.5.34 服务器。有谁知道,那里可能有什么问题?我花了一整天的时间才找到问题,但我真的不知道,现在该怎么办。我一直在谷歌和这里寻找解决方案,但我没有找到任何东西。提前感谢每一个有用的答案!
下面我列出了我所有项目文件的内容:
配置类 - SpringConfig.java
package com.smc.config;
import com.smc.controllers.HelloController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.util.List;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.smc"})
public class SpringConfig implements WebMvcConfigurer {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer defaultServletHandlerConfigurer) {
defaultServletHandlerConfigurer.enable();
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver bean =
new InternalResourceViewResolver();
bean.setPrefix("/web/");
bean.setSuffix(".html");
return bean;
}
@Bean
public HelloController helloController(){
return new HelloController();
}
//Of course there are empty implementations of the rest methods from interface
}
WebApplicationInitializer 接口的实现——ApplicationStarter.java 创建了一个调度器 servlet(我认为):
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class ApplicationStarter implements WebApplicationInitializer {
public void onStartup(ServletContext sc) throws ServletException {
AnnotationConfigWebApplicationContext ctx = getContext();
ctx.refresh();
ctx.setServletContext(sc);
sc.addListener(new ContextLoaderListener(ctx));
ServletRegistration.Dynamic appServlet = sc.addServlet("DispatcherServlet", new DispatcherServlet(ctx));
appServlet.setLoadOnStartup(1);
appServlet.addMapping("*.html");
}
private AnnotationConfigWebApplicationContext getContext(){
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(com.smc.config.SpringConfig.class);
return ctx;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>SystemMaintainContracts</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
简单控制器 - HelloController.java
package com.smc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value = "/hello")
public String showIndexPage(Model model){
model.addAttribute("hello", "hello from HelloController!");
return "hello";
}
}
这里你有我的项目结构: project structure
【问题讨论】:
标签: spring spring-mvc http-status-code-404