Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Spring 3.1 with No XML Not working

  1. #1
    Join Date
    Feb 2012
    Posts
    9

    Default Spring 3.1 with No XML Not working

    So I've setup a spring mvc 3.1 project from scratch and am not using any xml. Thing is my controllers are not getting registered. There are no errors what so ever its like the controller doesn't exist.

    The index page loads when I run the application but when I try the url thats mapped nothing happens.
    The console shows no errors and I have no build errors.

    My Initializer class looks like this:

    Code:
    @Configuration
    public class Initializer implements WebApplicationInitializer
    {
    public void onStartup(ServletContext container) throws ServletException
    	{
    		logger.info("Loading..");
    		System.out.print("YALLO");
    		AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    		rootContext.register(AppConfig.class);
    		
     
    		// Manage the lifecycle of the root application context
    		container.addListener(new ContextLoaderListener(rootContext));
     
    		// Create the dispatcher servlet's Spring application context
    		AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
    		dispatcherContext.register(DispatcherConfig.class);
     
    		// Register and map the dispatcher servlet
    		ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
    		dispatcher.setLoadOnStartup(1);
    		dispatcher.addMapping("/"); 
    	}
    }

    My configuration class looks like this:
    Code:
    @Configuration
    @ComponentScan(basePackages = "com.yardy.controllers")
    public class DispatcherConfig
    {
    	private static final Logger logger = LoggerFactory.getLogger(DispatcherConfig.class);
    	
    	@Bean
    	public InternalResourceViewResolver configureInternalResourceViewResolver()
    	{
    		logger.error("HELLO");
    		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    		resolver.setPrefix("/WEB-INF/views/");
    		resolver.setSuffix(".jsp");
    		return resolver;
    	}
    }
    Controller looks like this:


    Code:
    package com.yardy.controllers;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    @Controller
    public class YardyController
    {
    	 private static final Logger logger = LoggerFactory.getLogger(YardyController.class);
    	 
    	 @RequestMapping(value = "/yardy")
    	 public String yardy()
    	 {
    		 System.out.print("Testing..");
    		logger.info("Welcome home!");
    		System.out.print("YARDY");
    		return "yardy";
    	 }
    }

    /yardy just 404's thou.

    Can anyone give me any insight into this.
    Perhaps point out something I'm missing.
    Last edited by loki70x7; Feb 6th, 2012 at 06:50 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    FOr starters @Configuration on the WebApplicationInitializer is useless. Also why do you map the controller to /yardy? That would lead to a url of /yardy/yardy to trigger the controller (as the servlet is mapped to /yardy). So either map the servlet to / or the controller to /... I strongly suggest a read of the reference guide especially the web part on how urls and url mappings work with @RequestMapping).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Feb 2012
    Posts
    9

    Default

    Quote Originally Posted by Marten Deinum View Post
    FOr starters @Configuration on the WebApplicationInitializer is useless. Also why do you map the controller to /yardy? That would lead to a url of /yardy/yardy to trigger the controller (as the servlet is mapped to /yardy). So either map the servlet to / or the controller to /... I strongly suggest a read of the reference guide especially the web part on how urls and url mappings work with @RequestMapping).
    That was me twiddling around with the mappings.

    I've changed it to just / now so the url should be /yardy.
    In either case nothing happens thou and the error doesn't seem to be caused by url mappings.
    It's more like the controller just isn't getting loaded or the WebApplicationInitializer isn't getting triggered.

    Is there something more required to get this working?
    Last edited by loki70x7; Feb 6th, 2012 at 06:53 AM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Are you running in a Servlet 3.0 container (tomcat7, jetty 8?) else nothing is going to be booted...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Feb 2012
    Posts
    9

    Default

    Quote Originally Posted by Marten Deinum View Post
    Are you running in a Servlet 3.0 container (tomcat7, jetty 8?) else nothing is going to be booted...
    Yes I'm running it on a Tomcat 7 server.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Do you have a web.xml? Also check your logging if you see something starting or see errors, you might want to enable debug logging.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Feb 2012
    Posts
    9

    Default

    Quote Originally Posted by Marten Deinum View Post
    Do you have a web.xml? Also check your logging if you see something starting or see errors, you might want to enable debug logging.
    I removed the web.xml and there are no errors thrown in the console log. Normally if a request path is not mapped you would see it in the console logs but I don't see any errors at all. I've also set the log level on TC to FINEST and still don't see anything.

    My log output is as follow:

    Code:
    6-Feb-2012 10:13:00 AM org.apache.catalina.core.AprLifecycleListener init
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jre6\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Java\jdk1.6.0_26\bin;ANT_HOME%\bin;C:\Users\martin\Documents\Latitude\endeca-portal\tomcat-6.0.29\bin;.
    6-Feb-2012 10:13:00 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:ASpring3.1' did not find a matching property.
    6-Feb-2012 10:13:00 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-bio-8080"]
    6-Feb-2012 10:13:00 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
    6-Feb-2012 10:13:00 AM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 461 ms
    6-Feb-2012 10:13:00 AM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service Catalina
    6-Feb-2012 10:13:00 AM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/7.0.25
    6-Feb-2012 10:13:00 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-bio-8080"]
    6-Feb-2012 10:13:00 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["ajp-bio-8009"]
    6-Feb-2012 10:13:00 AM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 287 ms

  8. #8
    Join Date
    Feb 2012
    Posts
    9

    Default

    Also based on my reading I think I should see something like this in the console:

    INFO : org.springframework.web.SpringServletContainerInit ializer - Delegating ServletContext to the following WebApplicationInitializer instances: [pl.codeleak.springmvc31demo.config.Initializer@1b4 cd65]

    Meaning that the Initializer is going to be automatically bootstrapped by any Servlet 3.0 container
    Which I currently do not see.
    Anyone have any thoughts on why this would not occur?

  9. #9

    Default

    Add the "@EnableWebMvc" annotation to your DispatcherConfig class (along with @Configuration and @ComponentScan) to enable all the Spring MVC annotation-based configuration goodies.

  10. #10
    Join Date
    Feb 2012
    Posts
    9

    Default

    Quote Originally Posted by scottyfred View Post
    Add the "@EnableWebMvc" annotation to your DispatcherConfig class (along with @Configuration and @ComponentScan) to enable all the Spring MVC annotation-based configuration goodies.
    I added that but no luck there.

    Are there any specific jar files that have to be in the lib folder?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •