Results 1 to 10 of 19

Thread: Spring 3.1 with No XML Not working

Hybrid View

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

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

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

    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

Posting Permissions

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