在 Eclipse 项目中获取 HTTP 404 错误

Getting HTTP 404 Error in Eclipse project

我正在尝试学习如何 运行 服务器上的 Spring MVC 项目(在这方面还是很新),但我遇到了 HTTP 404 错误。我' m 在 Windows 7 机器上使用 Eclipse Luna 4.4.1、Java 7、Spring 4.1.4 和 Pivotal tc server 3.0.2。在我继续之前,让我说我在使用 Tomcat 7 创建一个简单的练习项目时遇到了同样的 404 错误,所以我认为问题出在我的服务器配置中(tc 服务器和 Tomcat).这是显示我的项目目录、我尝试过的服务器 运行ning 和错误页面的屏幕截图:

程序代码看起来不错。当我 运行 我的 HomeControllerTest(我基本上是从 Craig Walls 的 Spring in Action 的新第 4 版中获得的)时,它会编译并通过。我还需要在哪里查看才能解决此问题? ` 包裹 com.kwalker.practicewellness;

import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.kwalker.practicewellness.web.HomeController;

public class HomeControllerTest {

@Test
public void testHomePage() throws Exception {
    HomeController controller = new HomeController();
    MockMvc mockMvc = standaloneSetup(controller).build();

    mockMvc.perform(get("/")).andExpect(view().name("home"));

}

}

` 这是 HomeController class:

package com.kwalker.practicewellness.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

@RequestMapping(value="/", method=RequestMethod.GET)
public String home() {
    return "home";
}
}

这是 WellnessWebAppInitializer:

package com.kwalker.practicewellness.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WellnessWebAppInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { RootConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebConfig.class };
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}

}

这是 RootConfig:

package com.kwalker.practicewellness.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"practicewellness"}, 
            excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
public class RootConfig {

}

这是 WebConfig:

package com.kwalker.practicewellness.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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("practicewellness.web")
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    return resolver;
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

}

这是 home.jsp 页面:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Welcome</title>
<link rel="stylesheet"
        type="text/css"
        href="<c:url value="/resources/style.css"/>">
</head>
<body>
<h1>
Welcome to Vital Potential!  
</h1>

<P>  The time on the server is ${serverTime}. </P>
</body>
</html>

这是 Maven 构建的输出:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder    org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Practice Wellness 1.0.0-BUILD-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ practicewellness ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ practicewellness ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.954 s
[INFO] Finished at: 2015-01-04T10:37:59-07:00
[INFO] Final Memory: 8M/115M
[INFO] ------------------------------------------------------------------------

这是控制台上的输出:(只有一个警告,这与他们在我正在观看的培训视频中收到的警告相同,运行对他们来说没问题):

Jan 04, 2015 10:42:37 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to  'org.eclipse.jst.jee.server:Practice Wellness' did not find a matching property.
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.57
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Nov 3 2014 08:39:16 UTC
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.57.0
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 7
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.1
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JAVA_HOME:             C:\Program Files\Java\jdk1.7.0_71\jre
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.7.0_71-b14
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:            C:\Users\kyle\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Apache-Tomcat\apache-tomcat-7.0.57
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -    Dcatalina.base=C:\Users\kyle\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Apache-Tomcat\apache-tomcat-7.0.57
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: - Dwtp.deploy=C:\Users\kyle\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Apache-Tomcat\apache-tomcat-7.0.57\endorsed
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Jan 04, 2015 10:42:37 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production  environments was not found on the java.library.path: C:\Program   Files\Java\jdk1.7.0_71\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Orac le\Java\javapath;C:\Program Files (x86)\HP SimplePass 2011\x64;C:\Program Files (x86)\HP SimplePass  2011\;;C:\Program Files\Broadcom\Broadcom 802.11\Driver;;C:\Program Files\Common Files\Microsoft   Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows  Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\Common  Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\Common Files\Roxio  Shared.0\DLLShared\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\WIDCOMM\Bluetooth  Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program  Files\Broadcom\WHL\;C:\Program Files\Broadcom\WHL\syswow64;C:\Program  Files\Broadcom\WHL\SysWow64\;C:\Program Files\Broadcom\WHL\SysWow64\syswow64;C:\Program Files  (x86)\Intel\Services\IPT\;C:\Program Files\Java\jdk1.8.0_25\bin;C:\Program Files\Gradle\Gradle- 2.0\gradle-2.2.1\bin;.
Jan 04, 2015 10:42:37 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jan 04, 2015 10:42:37 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 911 ms
Jan 04, 2015 10:42:38 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 04, 2015 10:42:38 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.57
Jan 04, 2015 10:42:40 AM org.apache.catalina.core.ApplicationContext log
INFO: Spring WebApplicationInitializers detected on classpath:  [com.kwalker.practicewellness.config.WellnessWebAppInitializer@4eda480b]
Jan 04, 2015 10:42:40 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization  started
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing  Root WebApplicationContext: startup date [Sun Jan 04 10:42:40 MST 2015]; root of context hierarchy
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Registering annotated classes: [class com.kwalker.practicewellness.config.RootConfig]
 INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 524 ms
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization started
Jan 04, 2015 10:42:41 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing  WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Jan 04 10:42:41 MST  2015]; parent: Root WebApplicationContext
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Registering annotated classes: [class com.kwalker.practicewellness.config.WebConfig]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/**]  onto handler of type [class  org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter -  Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date   [Sun Jan 04 10:42:41 MST 2015]; parent: Root WebApplicationContext
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization completed in 1029 ms
Jan 04, 2015 10:42:42 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jan 04, 2015 10:42:42 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jan 04, 2015 10:42:42 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4348 ms

看起来你在所有配置中都错误地使用了 @ComponentScan - 基本包名称缺少 com.kwalker. 部分 - 我的猜测是组件扫描没有开始你的家庭控制器。

尝试将配置中的基础包更新为完整的包名。

此外,如果您以 INFO 日志记录模式启动服务器,您应该获得所有已注册 controller/paths 的详细信息 - 这可能会更清楚地说明它。