Eclipse动态Web模块3.0

时间:2015-06-12 15:34:12

标签: java spring-mvc tomcat7

我使用服务器上运行/运行的默认配置创建了Spring MVC项目,但是当我将 Project Facets 更改为 J2EE 6 时,如下所示: enter image description here
现在,当我运行项目时,我得到以下消息 enter image description here

更新
请看这个项目结构:
enter image description here
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";
}
}

新控制台输出没有任何错误:

  

2015年6月20日下午6:33:38 org.apache.catalina.core.ApplicationContext log   信息:初始化Spring根WebApplicationContext   INFO:org.springframework.web.context.ContextLoader - Root WebApplicationContext:初始化已启动   信息:org.springframework.web.context.support.XmlWebApplicationContext - 刷新根WebApplicationContext:启动日期[星期六6月20日18:33:38 PKT 2015];上下文层次结构的根   INFO:org.springframework.beans.factory.xml.XmlBeanDefinitionReader - 从ServletContext资源加载XML bean定义[/WEB-INF/spring/root-context.xml]   INFO:org.springframework.beans.factory.support.DefaultListableBeanFactory - 在org.springframework.beans.factory.support.DefaultListableBeanFactory@1a8f100中预先实例化单例:定义beans [];工厂层级的根   信息:org.springframework.web.context.ContextLoader - Root WebApplicationContext:初始化在893 ms完成   2015年6月20日下午6:33:39 org.apache.catalina.core.ApplicationContext日志   信息:初始化Spring FrameworkServlet' appServlet'   INFO:org.springframework.web.servlet.DispatcherServlet - FrameworkServlet' appServlet':初始化已启动   INFO:org.springframework.web.context.support.XmlWebApplicationContext - 为命名空间刷新WebApplicationContext' appServlet-servlet':启动日期[星期六6月20日18:33:39 PKT 2015]; parent:Root WebApplicationContext   INFO:org.springframework.beans.factory.xml.XmlBeanDefinitionReader - 从ServletContext资源加载XML bean定义[/WEB-INF/spring/appServlet/servlet-context.xml]   INFO:org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-250' javax.annotation.ManagedBean'找到并支持组件扫描   信息:org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330' javax.inject.Named'找到并支持组件扫描的注释   INFO:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330' javax.inject.Inject'找到并支持自动装配的注释   INFO:org.springframework.beans.factory.support.DefaultListableBeanFactory - 在org.springframework.beans.factory.support.DefaultListableBeanFactory@1c047f0中预实例化单例:定义bean [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,组织。 springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,组织。 springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.Resou rceHttpRequestHandler#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@1a8f100   INFO:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped" {[/],methods = [GET],params = [],headers = [],consume = [],产生= [〕,定制= []}"到public 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 ms完成

http://localhost:8080显示 HTTP状态404 在这种情况下的任何提示?我该怎么办?

1 个答案:

答案 0 :(得分:1)

你的问题与eclipse中的eclipse或动态web模块无关,除了存在多个问题。弹簧配置存在一般问题。

您正尝试通过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)

关于如何在spring.io论坛中定义映射到上下文根的更多类似问题,如"@RequestMapping pointing to /"

通过将Spring Dispatcher Servlet映射到“/”而不是“/ *”来存档它 并从@RequestMapping注释中删除显式映射到“/”。

shared的项目不是生成日志输出的项目。在Tomcat上部署和执行时,项目甚至都没有启动SpringContext。

要启动SpringContext,您必须在ContextLoadListener中配置DispatcherServlet和/或web.xml 像这里:

<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> 

或者您需要一个实现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("/");

    }

}

从您在项目中使用的位置加载弹簧配置文件(root-context.xmlservlet-context.xml)。

可以在github上找到使用WebApplicationInitializer的固定版本。我给你发了拉请求。

固定版本的控制台输出是:

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.

Screenshot of the rendered page

为了修复你的日食问题,我建议从eclipse中删除旧项目,然后从github重新导入修复版本。

在Eclipse中:

  • Servers视图中停止 Tomcat 实例。
  • 从Tomcat中删除部署(上下文菜单 - &gt;删除)
  • 删除项目(理想情况下将其从磁盘中删除,或将其移至其他位置)
  • 查看固定版本(我的或我的更改)
  • 重新导入固定版本(文件 - &gt;导入.... - &gt; Maven - &gt;现有版本 Maven项目)