Spring Web MVC, image says it all.
Front Controller- org.springframework.web.servlet.DispatcherServlet( Handles all incoming request)
DispatcherServlet LifeCycle:
init(): Read the init-param of web.xml and create a WebApplicationContext , to load beans.
service(): Each request runs in a seperate thread of its own and service method looks for HandlerMapping, if Controllers are annotation driven, then RequesMappingHandlerMapping will automatically map the request to a controller.Corresponding controller is called and it passes the flow to business logic to form Model object.Once the controller is finished it returns the JSP name to be called which is then resolved by ViewResolver and corresponding JSP is called. JSP processes and then the response is returned back.
destroy()
Validation in Spring:
1. Implicit finding of validators by Spring container using LocalValidatorFactoryBean to register validators if found on classpath(e.g Hibernate-Validator)
2. Custom Validation by implementing Validator interface.(ValidationUtils can be used for inbuilt validations)
3. Annotation Based Custom Validation by implementing ConstraintValidator interface.
Examples can be found on my Github page.
Spring Tags:
Two tag libraries:
1. spring.tld
2. spring-form.tld
Session Management:
https://dzone.com/articles/using-http-session-spring
MessageSource:
ResourceBundleMessageSource and ReloadableResourceBundleMessageSource
http://learningviacode.blogspot.com.au/2012/07/reloadable-messagesources.html
Logging:
Log4j
To integrate log4j, all you need to do is :
- Puts the
log4j.jarin the project classpath. - Create a
log4j.propertiesorlog4j.xmlfile in the project root classpath (if you follow the Maven standard directory structure, this should be theresourcesfolder).
http://www.mkyong.com/spring-mvc/spring-mvc-log4j-integration-example/
Custom arguments for @RequestMapping methods:
Use WebArgumentResolver
Steps:
1. Define a annotation say @UserName
2. Create a custom WebArgumentResolver class which implements WebArgumentResolver interface.
3. Wire in spring config xml file like:
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="customArgumentResolvers">
<list>
<bean class="com.raf.online.corporate.spring.UserNameWebArgumentResolver"/>
</list>
</property>
</bean>
http://www.cakesolutions.net/teamblogs/2009/10/23/custom-arguments-for-requestmapping-methods
Spring REST Services
@PathVariable is to obtain some placeholder from the uri (Spring call it an URI Template) — see Spring Reference Chapter 16.3.2.2 URI Template Patterns
@RequestParam is to obtain an parameter — see Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with @RequestParam
Assume this Url http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013 (to get the invoices for user 1234 for today)
@RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
public List<Invoice> listUsersInvoices(
@PathVariable("userId") int user,
@RequestParam(value = "date", required = false) Date dateOrNull) {
...
}
