Skip to main content

Spring MVC

++++++++++++++++++++++++++++
Spring Web MVC framework
++++++++++++++++++++++++++++
The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale, time zone and theme resolution as well as support for uploading files.

a) With the introduction of Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sites and applications, through the @PathVariable annotation and other features

b) Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

c) For Spring based validation at controller level - An @RequestBody method parameter can be annotated with @Valid, in which case it will be validated using the configured Validator instance.

d) @RestController is a stereotype annotation that combines @ResponseBody and @Controller. More than that, it gives more meaning to your Controller and also may carry additional semantics in future releases of the framework.

e) An @ModelAttribute on a method argument indicates the argument should be retrieved from the model. If not present in the model, the argument should be instantiated first and then added to the model.

f) As a result of data binding there may be errors such as missing required fields or type conversion errors. To check for such errors add a BindingResult argument immediately following the @ModelAttribute argument:

The spring-test module offers first class support for testing annotated controllers.

h)Interceptors located in the handler mapping must implement HandlerInterceptor from the org.springframework.web.servlet package. This interface defines three methods: preHandle(..) is called before the actual handler is executed; postHandle(..) is called after the handler is executed; and afterCompletion(..)

i) RedirectView
One way to force a redirect as the result of a controller response is for the controller to create and return an instance of Spring’s RedirectView. In this case, DispatcherServlet does not use the normal view resolution mechanism. Rather because it has been given the (redirect) view already, the DispatcherServlet simply instructs the view to do its work.

The RedirectView issues an HttpServletResponse.sendRedirect() call that returns to the client browser as an HTTP redirect. By default all model attributes are considered to be exposed as URI template variables in the redirect URL. Of the remaining attributes those that are primitive types or collections/arrays of primitive types are automatically appended as query parameters.

Comments

Popular posts from this blog

Spring OXM

Spring's Object/XML Mapping support : Converting an XML document to and from an object. This conversion process is also known as XML Marshalling, or XML Serialization. a marshaller is responsible for serializing an object (graph) to XML an unmarshaller deserializes the XML to an object graph <oxm:jaxb2-marshaller id="anyWSMarshaller" contextPath="com.utkarsh.org.phase2.jaxbgenerated"/> <bean id="anyServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"> <property name="marshaller" ref="acbsPaymentsAdvancesWSMarshaller"/> <property name="unmarshaller" ref="acbsPaymentsAdvancesWSMarshaller"/> <property name="defaultUri" value="http://localhost:8080/SpringOXMApp/anyUrl/testService"/> <property name="messageSenders">  <list>   <ref bean="httpMsgSende

Spring Security with Boot - Adding LDAP Over Already Authenticated User from One Module

There are hundreds of Spring Tutorials and Stack of Questions/Answers online to add LDAP based Authentication in your web application using Spring Security However, while working on one of my project assignments, I faced a strange conundrum whereas per the requirements of the application the REST-based application is required to be accessed from within a spring based web application of which this Rest API app. will be part of and also independently hosted for outside application or for 3rd Party Access. Let's make some assumptions here for the sake of better understanding the issue/s and resolution/s Main web-app as MWAPP Rest Based API Module as RESTAPP MWAPP is based on Spring MVC with Spring Security and all configuration based RESTAPP is Spring Boot based Simple Web App. Issues:: MWAPP is having LDAP Security using spring config xml file as users can use this app with some features which does nt need API exposed in RESTAPP hence user logs in and is LDAP authe