尝试了数周之后,才尝试启动并运行Spring MVC App并使用Jetty服务器。问题是,即使它可以找到WebApplicationInitializers并且日志没有抛出任何可疑的信息,它也会给我一个Error 404 Not Found。好像它不能或不会听我定义的控制器。这是一个没有XML的纯Java配置Spring MVC webapp
我正在使用这些指南:
http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example-annotation/
和
https://www.mkyong.com/spring-mvc/gradle-spring-4-mvc-hello-world-example-annotation/
我的POM像这样配置jetty-maven-plugin:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<wtpversion>2.0</wtpversion>
<wtpContextName></wtpContextName>
</configuration>
</plugin>
以下是我的目录结构:
扫描文件只是告诉Spring扫描co.uk.naz.cv中的所有内容,以便在子程序包中获取批注。
所有其他都是典型配置:
我的WebConfig类:
@Configuration
@EnableWebMvc
public class WebConfig
{
@Bean
public ViewResolver viewResolver()
{
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/jsp/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/js/**", "/css/**", "/images/**")
.addResourceLocations("/resources/core/js/", "/resources/core/css/", "/resources/core/css/images")
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
}
我的RootConfig:
@Configuration
@ComponentScan(excludeFilters = { @Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class) })
public class RootConfig
{
//Class is intentionally left empty
}
WebAppInitialiser:
public class WebAppInitialiser extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override
protected String[] getServletMappings() { return new String[] { "/" }; }
@Override
protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { RootConfig.class }; }
@Override
protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { WebConfig.class }; }
}
和简单的控制器:
@Controller
public class HomeController
{
@GetMapping("/")
public String home() { return "index"; };
}
这一直是一场噩梦,而离我最近的是它正在获得目录列表,该目录列表使我可以导航到html文件。
希望这是一个简单的解决方法,但是我一生无法看到这里出了什么问题。我想要的只是让此WebApp返回index.jsp文件。我已经仔细检查了所有路径,并确定它们是正确的。
为完成此操作,请参阅日志:
[INFO] Logging initialized @1353ms to org.eclipse.jetty.util.log.Slf4jLog
[INFO] webAppSourceDirectory not set. Trying src/main/webapp
[INFO] Reload Mechanic: automatic
[INFO] nonBlocking:false
[INFO] Classes = /home/naz/Eclipse/Projects/cv/target/classes
[INFO] Configuring Jetty for project: cv Maven Webapp
[INFO] Context path = /
[INFO] Tmp directory = /home/naz/Eclipse/Projects/cv/target/tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = null
[INFO] Webapp directory = /home/naz/Eclipse/Projects/cv/src/main/webapp
[INFO] jetty-9.4.11.v20180605; built: 2018-06-05T18:24:03.829Z; git: d5fc0523cfa96bfebfbda19606cad384d772f04c; jvm 1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11
[INFO] Scanning elapsed time=148ms
[INFO] 1 Spring WebApplicationInitializers detected on classpath
[INFO] DefaultSessionIdManager workerName=node0
[INFO] No SessionScavenger set, using defaults
[INFO] node0 Scavenging every 660000ms
[INFO] Initializing Spring root WebApplicationContext
[INFO] Initializing Spring FrameworkServlet 'dispatcher'
[INFO] Started o.e.j.m.p.JettyWebAppContext@2a22ad2b{/,file:///home/naz/Eclipse/Projects/cv/src/main/webapp/,AVAILABLE}{file:///home/naz/Eclipse/Projects/cv/src/main/webapp/}
[INFO] Started ServerConnector@33f2cfda{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
[INFO] Started @2681ms
[INFO] Started Jetty Server