【问题标题】:HTTP Status 404 Spring MVCHTTP 状态 404 Spring MVC
【发布时间】:2016-01-08 18:25:57
【问题描述】:

我已经阅读了许多与我类似的问题,但我还没有找到解决方案。我在eclipse中创建了这个项目,它工作正常。之后,我将它上传到 github,然后从我的 PC 中删除了它。后来,我将它从 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

这是我的代码:

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

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

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


}

【问题讨论】:

    标签: java eclipse spring maven tomcat


    【解决方案1】:

    您将此应用程序部署到 tomcat 的名称是什么?错误消息表明它应该是springProject。请注意,Tomcat 也支持大写字母 - 如果您没有使用其他部署技术,它应该在 Tomcat 的webapps/springProject 中。如果您已通过 eclipse 进行部署(因为您使用 eclipse 标记此问题),只需确保这是您的项目的名称,以及“服务器”视图中显示的名称。

    另外,检查 tomcat 的日志是否有任何问题 - 例如。可能存在阻止您查看此项目的部署问题。

    【讨论】:

    • 项目以正确的名称保存,但问题是当我从github导入新项目时,Eclipse中的tomcat服务器已经运行。当我重新启动 tomcat 服务器时,错误消失了。
    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    相关资源
    最近更新 更多