unable to load home view using AbstractAnnotationConfigDispatcherServletInitializer in Spring MVC

StackOverflow https://stackoverflow.com/questions/19247365

  •  30-06-2022
  •  | 
  •  

Question

i am new to Spring MVC, i have created spring 3.2 project using maven in eclipse. i am implementing AbstractAnnotationConfigDispatcherServletInitializer class for java based configuration.

i have following plugins and dependencies in pom.xml

<build>
        <plugins>
            <!--source level should be 1.6 (which is not Maven default) for java EE 
                6 projects, so let's change it -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <!-- When using xml-less approach, you need to disable Maven's warning 
                about missing web.xml -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
      <!--We need servlet API for compiling the classes. Not needed in runtime
-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--adding spring mvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.4.RELEASE</version>            
        </dependency>       
        <!-- Add Taglib support -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>           
    </dependencies>

Initializer class is as......

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebappConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }       
}

WebappConfig class is as......

@Configuration
@ComponentScan(basePackages={"com.sandip.controllers"})
@EnableWebMvc
public class WebappConfig extends WebMvcConfigurerAdapter{


    //add view Resolver, Tell SpingMVC where to find view scripts
        @Bean
        public InternalResourceViewResolver setupViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");

            return resolver;
        }
}

and finally i have a HomeController as..........

@Controller
public class HomeContoller {      

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String home() {
    return "home";
  }  
}

when i run this my project with jetty after maven build it display the following output in browser.....

enter image description here

springZeroXml is my project name there is no errro in console, please help ..........

Was it helpful?

Solution

I did lot of googleing and find out i need to override the addViewControllers method of WebMvcConfigurerAdapter class in WebAppConfig. and code is..........

@Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("home");
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top