Wednesday, September 19, 2012

Sample Web Application using Spring MVC Framework

This is the Sample Web Application using Spring MVC Framework. Run this application to make sure the environment is working.

Environment:
Jdk1.7.07
Maven 3.0.4
Spring Framework version 3.1.1.RELEASE
Spring Source Tools Suits(STS) 3.0

Download Source code here
To open the source code:
File --> Import --> Maven --> Existing Maven Projects


If you are new to Maven, please visit  Maven Tutorial.




HomeController.java
package org.sample.app;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@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(value = "/", method = RequestMethod.GET)
 public String home(Locale locale, Model model) {
  logger.info("Welcome home! the client locale is "+ locale.toString());
  
  Date date = new Date();
  DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
  
  String formattedDate = dateFormat.format(date);
  
  model.addAttribute("serverTime", formattedDate );
  
  return "home";
 }
 
}
log4j.xml




 
 
  
  
   
  
 
 
 
 
  
 
 
 
 
  
 
 
 
  
 
 
 
  
 

 
  
 

 
 
  
  
 
 

servlet-context.xml



 
 
 
 

 
 

 
 
  
  
 
 
 
 
 
 


root-context.xml


 
 
  

web.xml



 
 
  contextConfigLocation
  /WEB-INF/spring/root-context.xml
 
 
 
 
  org.springframework.web.context.ContextLoaderListener
 

 
 
  appServlet
  org.springframework.web.servlet.DispatcherServlet
  
   contextConfigLocation
   /WEB-INF/spring/appServlet/servlet-context.xml
  
  1
 
  
 
  appServlet
  /
 



pom.xml


 4.0.0
 org.sample
 app
 Sample Web Application using Spring Framework
 war
 1.0.0-BUILD-SNAPSHOT
 
  3.1.1.RELEASE
  1.6.9
  1.5.10
 
 
  
  
   org.springframework
   spring-context
   ${org.springframework-version}
   
    
    
     commons-logging
     commons-logging
     
   
  
  
   org.springframework
   spring-webmvc
   ${org.springframework-version}
  
    
  
  
   org.aspectj
   aspectjrt
   ${org.aspectj-version}
   
  
  
  
   org.slf4j
   slf4j-api
   ${org.slf4j-version}
  
  
   org.slf4j
   jcl-over-slf4j
   ${org.slf4j-version}
   runtime
  
  
   org.slf4j
   slf4j-log4j12
   ${org.slf4j-version}
   runtime
  
  
   log4j
   log4j
   1.2.15
   
    
     javax.mail
     mail
    
    
     javax.jms
     jms
    
    
     com.sun.jdmk
     jmxtools
    
    
     com.sun.jmx
     jmxri
    
   
   runtime
  

  
  
   javax.inject
   javax.inject
   1
  
    
  
  
   javax.servlet
   servlet-api
   2.5
   provided
  
  
   javax.servlet.jsp
   jsp-api
   2.1
   provided
  
  
   javax.servlet
   jstl
   1.2
  
 
  
  
   junit
   junit
   4.7
   test
          
 
    
        
            
                maven-eclipse-plugin
                2.9
                
                    
                        org.springframework.ide.eclipse.core.springnature
                    
                    
                        org.springframework.ide.eclipse.core.springbuilder
                    
                    true
                    true
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    1.7
                    1.7
                    -Xlint:all
                    true
                    true
                
            
            
                org.codehaus.mojo
                exec-maven-plugin
                1.2.1
                
                    org.test.int1.Main
                
            
        
    




Result:









Log
情報: 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 [Wed Sep 19 21:14:35 JST 2012]; 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-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@55590e: 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@4820da
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.sample.app.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 1469 ms


INFO : org.sample.app.HomeController - Welcome home! the client locale is ja
9 19, 2012 9:03:55 PM org.apache.jasper.compiler.TldLocationsCache tldScanJar
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.

No comments:

Post a Comment