简体   繁体   English

HTTP状态404 Spring MVC

[英]HTTP Status 404 Spring MVC

I've read many questions which are similar to mine, but I haven't found the solution. 我已经阅读了许多与我的问题类似的问题,但尚未找到解决方案。 I created this project in eclipse and it was working properly. 我在eclipse中创建了这个项目,它运行正常。 Afterwards, I uploaded it to github, deleted it from my PC. 之后,我将其上传到github,从我的PC中删除了它。 Later, I cloned it from github to my PC, and I used "import existing maven project" and that loaded the project in eclipse. 后来,我将其从github克隆到PC上,并使用“导入现有的maven项目”,并将该项目加载到eclipse中。 But when I run it now I get the following error (I added the project into tomcat server): 但是当我现在运行它时,出现以下错误(我将项目添加到了Tomcat服务器中):

HTTP Status 404 - /springProject/
type Status report

message /springProject/

description The requested resource is not available.


--------------------------------------------------------------------------------

Apache Tomcat/8.0.30

Here are my codes: 这是我的代码:

web.xml web.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 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/");
    }


}

HomeController.java HomeController.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");
    }


}

What's the name that you deploy this application to tomcat as? 您将此应用程序部署到tomcat的名称是什么? The error message suggests that it should be springProject . 错误消息表明它应该是springProject Note that Tomcat also honors capital letters - so it should be in tomcat's webapps/springProject if you haven't used another deployment technique. 请注意,Tomcat也采用大写字母-因此,如果您未使用其他部署技术,则它应位于tomcat的webapps/springProject If you've deployed through eclipse (as you're tagging this question with eclipse), just make sure that this is the name of your project, and the name that's shown in the "Server" view. 如果您是通过eclipse部署的(用eclipse标记此问题),只需确保这是项目的名称,以及在“服务器”视图中显示的名称。

Also, check tomcat's logs for any problems - eg there might have been a deployment problem that prohibits you seeing this project. 另外,请检查tomcat的日志中是否存在任何问题,例如,可能存在禁止您查看该项目的部署问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM