简体   繁体   English

Eclipse动态Web模块3.0的Apache tomcat问题

[英]Apache tomcat issue with Eclipse dynamic web module 3.0

I created Spring MVC project with default configurations it was working/running on the server but when I changed Project Facets to J2EE 6 as following : 我使用默认配置创建了Spring MVC项目,该默认配置正在服务器上运行/运行,但是当我将Project Facets更改为J2EE 6时 ,如下所示: 在此处输入图片说明
now when I run the project I get following message 现在,当我运行项目时,我收到以下消息
在此处输入图片说明

UPDATE UPDATE
Please see this project structure : 请查看此项目结构:
在此处输入图片说明
Controller.java is Controller.java

@Controller
public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );

    return "home";
}
}

New Console out put without any error is : 没有出现任何错误的新控制台输出是:

Jun 20, 2015 6:33:38 PM 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.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sat Jun 20 18:33:38 PKT 2015]; 2015年6月20日下午6:33:38 org.apache.catalina.core.ApplicationContext日志INFO:初始化Spring根WebApplicationContext INFO:org.springframework.web.context.ContextLoader-根WebApplicationContext:初始化已启动INFO:org.springframework.web .context.support.XmlWebApplicationContext-刷新根WebApplicationContext:启动日期[PKT 2015年6月20日星期六18:33:38]; root of context hierarchy INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/root-context.xml] INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1a8f100: defining beans []; 上下文层次结构的根INFO:org.springframework.beans.factory.xml.XmlBeanDefinitionReader-从ServletContext资源[/WEB-INF/spring/root-context.xml]加载XML bean定义INFO:org.springframework.beans.factory.support .DefaultListableBeanFactory-在org.springframework.beans.factory.support.DefaultListableBeanFactory@1a8f100中预先实例化单例:定义bean []; root of factory hierarchy INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 893 ms Jun 20, 2015 6:33:39 PM org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring FrameworkServlet 'appServlet' INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Sat Jun 20 18:33:39 PKT 2015]; 工厂层次结构的根INFO:org.springframework.web.context.ContextLoader-根WebApplicationContext:初始化于893毫秒完成于2015年6月20日org.apache.catalina.core.ApplicationContext日志INFO:初始化Spring FrameworkServlet' appServlet'INFO:org.springframework.web.servlet.DispatcherServlet-FrameworkServlet'appServlet':初始化已开始INFO:org.springframework.web.context.support.XmlWebApplicationContext-刷新名称空间'appServlet-servlet'的WebApplicationContext:启动日期[Sat Jun 20 18:33:39 PKT 2015]; parent: Root WebApplicationContext INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml] INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1c047f0: defining beans [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.spri 父:根WebApplicationContext信息:org.springframework.beans.factory.xml.XmlBeanDefinitionReader-从ServletContext资源[/WEB-INF/spring/appServlet/servlet-context.xml]加载XML bean定义信息:org.springframework.context.annotation .ClassPathBeanDefinitionScanner-找到并支持组件扫描INFO的JSR-250'javax.annotation.ManagedBean'-org.springframework.context.annotation.ClassPathBeanDefinitionScanner-找到并支持JSR-330'javax.inject.Named'注释用于组件扫描INFO: org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor-找到并支持自动装配INFO的JSR-330'javax.inject.Inject'标注:org.springframework.beans.factory.support.DefaultListableBeanFactory-在org.springframework中预先实例化单例.beans.factory.support.DefaultListableBeanFactory @ 1c047f0:定义bean [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.spri ngframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,homeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.inter ngframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet。 mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet。 handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet。 handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,HomeController中,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.inter nalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; nalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor $ ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@1a8f100 INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String home.com.web.HomeController.home(java.util.Locale,org.springframework.ui.Model) INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0' INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 2619 ms 父:org.springframework.beans.factory.support.DefaultListableBeanFactory@1a8f100信息:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping-映射“ {[/],方法= [GET],params = [] ,headers = [],consumes = [],produces = [],custom = []}“到公共java.lang.String home.com.web.HomeController.home(java.util.Locale,org.springframework.ui .Model)INFO:org.springframework.web.servlet.handler.SimpleUrlHandlerMapping-将URL路径[/ resources / **]映射到处理程序“ org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0” INFO:org.springframework。 web.servlet.DispatcherServlet-FrameworkServlet'appServlet':初始化在2619毫秒内完成

with this at http://localhost:8080 also shows HTTP Status 404 Any hint in this case? http:// localhost:8080处显示的HTTP状态404在这种情况下有任何提示吗? What should I do? 我该怎么办?

Your problem has nothing to do with eclipse or dynamic web modules in eclipse, except there exists more than one problem. 您的问题与eclipse中的eclipse或动态Web模块无关,除非存在多个问题。 You have a general problem in your spring configuration. 您的spring配置中有一个普遍的问题。

You are trying to map '/' via RequestMapping - that does not work even in in Spring MVC 4.1.6 - independent of eclipse or dynamic web module. 您正在尝试通过RequestMapping映射“ /”-即使在Spring MVC 4.1.6中也不起作用-独立于Eclipse或动态Web模块。

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
- Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}"
onto public java.lang.String sd.sdse.dsdds.HomeController.home(java.util.Locale,org.springframework.ui.Model)

There are more similar questions about how define a mapping to the context root in in the spring.io forum like "@RequestMapping pointing to /" 在spring.io论坛中,还有更多有关如何定义到上下文根的映射的类似问题,例如“ @RequestMapping指向/”。

You archive this by mapping the Spring Dispatcher Servlet to "/" not "/*" and by removing the explicit mapping to "/" from your @RequestMapping annotations. 通过将Spring Dispatcher Servlet映射到“ /”而不是“ / *”,并从@RequestMapping批注中删除对“ /”的显式映射,可以对此进行归档。

The Project you've shared is not the one which generates the log output. 共享的项目不是生成日志输出的项目。 The project not even starts a SpringContext when deployed and executed on Tomcat. 在Tomcat上部署并执行该项目后,它甚至不会启动SpringContext。

To start the SpringContext you'll either have to configure the ContextLoadListener and / or DispatcherServlet in the web.xml like here: 要启动SpringContext,您必须在web.xml配置ContextLoadListener和/或DispatcherServlet ,如下所示:

<display-name>Home</display-name>
   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/servlet-config.xml</param-value>
</context-param>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 

Or you need a class that implements the WebApplicationInitializer interface like this one: 或者,您需要一个实现WebApplicationInitializer接口的类,如下所示:

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext context) {
        XmlWebApplicationContext rootContext = 
                new XmlWebApplicationContext();
        rootContext.setConfigLocation("/WEB-INF/spring/root-context.xml");

        context.addListener(new ContextLoaderListener(rootContext));

        // Create the dispatcher servlet's Spring application context
        XmlWebApplicationContext servletContext = 
                new XmlWebApplicationContext();
        servletContext.setConfigLocation("/WEB-INF/spring/appServlet/servlet-context.xml");

        // add the dispatcher servlet and map it to /
        ServletRegistration.Dynamic dispatcher = 
                context.addServlet("springDispatcher", new DispatcherServlet(servletContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }

}

which loads the spring configuration files( root-context.xml , servlet-context.xml ) from the locations you've used in your project. 它从您在项目中使用的位置加载spring配置文件( root-context.xmlservlet-context.xml )。

The fixed version which uses the WebApplicationInitializer can be found on github . 使用WebApplicationInitializer的固定版本可以在github上找到。 I've sent you a pull request. 我已向您发送请求请求。

The console output of the fixed version is: 固定版本的控制台输出为:

INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@76dc331c: defining beans [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,homeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@5c23f9fd
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.dom.son.HomeController.home(java.util.Locale,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'springDispatcher': initialization completed in 430 ms
Jun 21, 2015 12:45:28 PM org.apache.coyote.AbstractProtocol start
Information: Starting ProtocolHandler ["http-bio-8080"]
Jun 21, 2015 12:45:28 PM org.apache.coyote.AbstractProtocol start
Information: Starting ProtocolHandler ["ajp-bio-8009"]
Jun 21, 2015 12:45:28 PM org.apache.catalina.startup.Catalina start
Information: Server startup in 2048 ms
INFO : com.dom.son.HomeController - Welcome home! The client locale is de_DE.

呈现页面的屏幕截图

In order to fix your eclipse problem I would suggest to delete the old project from eclipse and re-import the fixed version from github. 为了解决您的日食问题,我建议从eclipse中删除旧项目,然后从github重新导入固定版本。

In Eclipse: 在Eclipse中:

  • In the Servers view stop your Tomcat instances. 在“ Servers视图中,停止Tomcat实例。
  • Remove the deployments from your Tomcat (context menu - > delete) 从Tomcat中删除部署(上下文菜单->删除)
  • Delete the Project (ideally remove it from disk, or move it to a different location) 删除项目(最好将其从磁盘上删除,或将其移动到其他位置)
  • Check out the fixed version (mine or yours with my changes) 检出固定版本(我的变更是我的还是您的,我的变更)
  • Re-import the fixed version (File -> Import.... -> Maven -> Existing Maven Project) 重新导入固定版本(文件->导入....-> Maven->现有Maven项目)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM