1
votes

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 : enter image description here
now when I run the project I get following message
enter image description here

UPDATE
Please see this project structure :
enter image description here
Controller.java is

@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]; 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 []; 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]; 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.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@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

with this at http://localhost:8080 also shows HTTP Status 404 Any hint in this case? What should I do?

1
What does tomcat log contain? Any error that may point to the reason?m4ktub
1/ The server starts and the application context is loaded correctly ? 2/ Did you check that the deployment assembly was correct?Michael Laffargue
Please see my update with console outputArshad Ali
I am also using same version , facing the same issues,it would be much helpful if any body deals with this issuesgks
What does it mean "Apache Tomcat 7 does not work when Project Facet is set to Dynamic Web Module 3.0 and localhost is also not working I need a solid answer to resolve this problem. " ? Which errors do you get. Btw. having a problem with the "Eclipse Dynamic Web Facette" or the Tomcat Servers is a complete different problem than the one described in your question.andih

1 Answers

1
votes

Your problem has nothing to do with eclipse or dynamic web modules in eclipse, except there exists more than one problem. You have a general problem in your spring configuration.

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.

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

You archive this by mapping the Spring Dispatcher Servlet to "/" not "/*" and by removing the explicit mapping to "/" from your @RequestMapping annotations.

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.

To start the SpringContext you'll either have to configure the ContextLoadListener and / or DispatcherServlet in the web.xml like here:

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

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.

The fixed version which uses the WebApplicationInitializer can be found on 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.

Screenshot of the rendered page

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.

In Eclipse:

  • In the Servers view stop your Tomcat instances.
  • Remove the deployments from your Tomcat (context menu - > delete)
  • 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)