我已经阅读了很多与我类似的问题,但我还没有找到解决方案。我在 eclipse 中创建了这个项目,它运行正常。之后,我将它上传到 github,从我的电脑上删除了它。后来,我将它从 github 克隆到我的 PC,我使用了“导入现有的 maven 项目”,并在 eclipse 中加载了该项目。但是当我现在运行它时出现以下错误(我将项目添加到 tomcat 服务器中):
HTTP Status 404 - /springProject/
type Status report
message /springProject/
description The requested resource is not available.
--------------------------------------------------------------------------------
Apache Tomcat/8.0.30
这是我的代码:
网络.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>springProject</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>springProject.springProject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
MvcConfiguration.java
package springProject.springProject.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan(basePackages="springProject.springProject")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
家庭 Controller .java
package springProject.springProject.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@RequestMapping(value="/")
public ModelAndView test(HttpServletResponse response) throws IOException{
return new ModelAndView("home");
}
}
最佳答案
您将此应用程序部署到 tomcat 的名称是什么?报错提示应该是springProject
。请注意,Tomcat 也支持大写字母 - 因此如果您没有使用其他部署技术,它应该在 tomcat 的 webapps/springProject
中。如果您已通过 eclipse 进行部署(因为您正在使用 eclipse 标记此问题),只需确保这是您的项目名称,以及显示在“服务器” View 中的名称。
此外,检查 tomcat 的日志是否有任何问题 - 例如可能存在禁止您查看此项目的部署问题。
关于java - HTTP 状态 404 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34683662/