我正在尝试学习如何在服务器上运行Spring MVC项目(此时仍然很新),我收到了一个http 404错误..我正在使用Eclipse Luna 4.4.1,Java 7 ,Windows 4.1机器上的Spring 4.1.4和Pivotal tc server 3.0.2。在我走得更远之前,让我说在使用Tomcat 7创建一个简单的练习项目时我得到了相同的404错误,所以我认为问题出在我的服务器配置中(在tc服务器和Tomcat中)。以下是显示我的项目目录,我尝试运行的服务器以及错误页面的屏幕截图:
程序代码似乎很好。当我运行我的HomeControllerTest(我基本上是从Craig Walls的第四版Spring in Action中获得)时,它会编译并通过。我还需要在哪里解决此问题? ` 包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类:
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 build的输出:
[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] ------------------------------------------------------------------------
以下是控制台上的输出:(只有一个WARNING,它与我正在观看的训练视频上的警告相同,对他们来说运行正常):
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\12.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
答案 0 :(得分:1)
在您的所有配置中,您似乎错误地使用了@ComponentScan
- 基本软件包名称缺少com.kwalker.
部分 - 我的猜测是组件扫描不是&n拿起你的家庭控制器。
尝试将配置中的基本软件包更新为完整的软件包名称。
此外,如果您在INFO日志记录模式下启动服务器,您应该获得已注册的所有控制器/路径的详细信息 - 这可能会对其有所了解。