Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Web applications with AnnotationConfigWebApplicationContext

    • 0
    • 2
    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 706
    Comment on it

    Web applications with AnnotationConfigWebApplicationContext: In Spring we can use the WebApplicationInitializer in place of web.xml to initialize DispatcherServlet and use @EnableWebMvc annotation to support Spring MVC.The below example code can show how we can use the WebApplicationInitializer Interface and @EnableWebMvc annotation instead of web.xml and dispatcher-servlet.xml.

    HelloController.java

    package com.babita; 
    
    import org.springframework.stereotype.Controller; 
    import org.springframework.ui.Model; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.servlet.ModelAndView; 
    
    
    @Controller 
    public class HelloController { 
    
        @RequestMapping("/login") 
        public ModelAndView hello(Model model) { 
    
                model.addAttribute("msg", "Hello World from Spring"); 
                return new ModelAndView("result"); 
        } 
    }
    

    WebAppInitializer.java

    package com.babita.config; 
    
    import javax.servlet.ServletContext;  
    import javax.servlet.ServletException;  
    import javax.servlet.ServletRegistration.Dynamic;  
    
    import org.springframework.web.WebApplicationInitializer;  
    import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;  
    import org.springframework.web.servlet.DispatcherServlet;  
    
    public class WebAppInitializer implements WebApplicationInitializer { 
    
        public void onStartup(ServletContext servletContext) throws ServletException 
        {  
            AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 
            ctx.refresh(); 
            ctx.register(AppConfig.class);  
            ctx.setServletContext(servletContext);    
            Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));  
            dynamic.addMapping("/");  
            dynamic.setLoadOnStartup(1);  
       }  
    } 
    

    AppConfig.java

    package com.babita.config;  
    
    import org.springframework.context.annotation.Bean; 
    import org.springframework.context.annotation.ComponentScan; 
    import org.springframework.context.annotation.Configuration; 
    import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
    import org.springframework.web.servlet.view.JstlView; 
    import org.springframework.web.servlet.view.UrlBasedViewResolver; 
    
    @Configuration 
    @ComponentScan("com.babita") 
    @EnableWebMvc   
    public class AppConfig {  
        @Bean  
        public UrlBasedViewResolver setupViewResolver() {  
            UrlBasedViewResolver resolver = new UrlBasedViewResolver();  
            resolver.setPrefix("/WEB-INF/jsp/");  
            resolver.setSuffix(".jsp");  
            resolver.setViewClass(JstlView.class);  
            return resolver;  
        } 
    
    }  
    

    Now define the jsp to show the view: result.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Spring4 MVC</title>
    </head>
    <body>
    <h1>${msg}</h1>
    
    </body>
    </html>
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: