[英]Running Tomcat from within Eclipse always gives me 404 (but index.html works)
首先是我的软件堆栈:
我正在使用没有web.xml的 servlet 3。
我做了什么:
javax.servlet.ServletContainerInitializer
,并将其放入实现WebApplicationInitializer
的完全限定的类名中 目前,我的项目包含约7个文件(3x .java,1x .jsp,1x .html,1x .pom,1x javax.servlet.ServletContainerInitializer)
什么有效:
通过Maven进行编译和打包,然后将.war部署在独立的Tomcat上。
http://127.0.0.1/MyApp/
显示index.html http://127.0.0.1/MyApp/hello
显示hello.jsp 什么不起作用:
如果我现在尝试通过Run -> Run on Server
使用Eclipse。 然后,选择我的Tomcat目录,等等。servlet( /hello
)仅给出404。但是index.html可以工作。
3个Java文件:
Initializer.java:
public class Initializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebAppConfig.class);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
WebAppConfig.java:
@Configuration
@ComponentScan("myapp.server")
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
FooController.java:
@Controller
public class FooController {
@RequestMapping("/hello")
public String helloWorld(Model model) {
model.addAttribute("wisdom", "Goodbye XML");
return "hello";
}
}
如果您需要更多其他信息,请告诉我。
谢谢!!
编辑:如果已通过Eclipse启动了Tomcat,我在哪里可以找到它的日志文件?
Eclipse控制台(所有这些都是在启动Tomcat之后出现的,即使我调用了一些URL,例如http://localhost:8080/
或http://localhost:8080/MyApp/
,它也不会更改 ): http : //pastebin.com / gGn0j48T
Tomcat日志似乎保存在.metadata/.plugins/org.eclipse.wst.server.core/tmp0/logs/
。 只有一个文件localhost_access_log.2013-02-20.txt
,输出看起来并不有趣: http : //pastebin.com/QmD4wPmA
现在我这样做是为了获得catalina.out
: https : //stackoverflow.com/a/5045247/1321564
这是重新启动Tomcat并尝试一些URL之后的输出:该文件保持为空...?!
我还意识到,以下目录在Tomcat上为空: wtpwebapps/MyApp/WEB-INF/lib/
。 那里不应该有一些Spring库吗?
得到它的工作!
如上所述: WEB-INF/lib
文件夹中没有Spring库。
而且因为我正在使用Maven。 解决方案非常简单:
Properties
Deployment Assembly
Add...
Java Build Path Entries
Next
Maven Dependencies
Finish
重新启动服务器。
现在,我可以在服务器视图中看到不同之处:
spring-web-3.2.1.RELEASE.jar
。 以前不是这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.