Spring MVC
What is the diff b/w struts and spring?
Struts
1. Here any req goes to ActionServlet
2. Here we have action classess
3. In struts we have form
4. Here form contains the validate()method
5. In struts we have <action-mappings>
6. Here we have ActionForward
7.In struts we have execute() in action classes
8. In struts 2.0 we Result classless
Spring
1.In spring any req goes to DispatcherServlet
2.Here we have controller classes
3. Here we have javaBeans(model)
4. Here JavaBeans doesn’t contain validate()
5. Here we have Handler Mappings…….</bean>
6. Here we have ModelAndView
7.Here every controller classes having the handleRequest()
8.Here we have viewReslover and view
What are the advantages of Spring MVC over Struts MVC ?
1. There is clear separation between models, views and controllers in Spring.
2. Spring’s MVC is very versatile and flexible based on interfaces but Struts forces
Actions and Form object into concrete inheritance.
3. Spring provides both interceptors and controllers..
To see Spring MVC hello world example
What is frontController?
A front controller is defined as “a controller which handles all requests for a Web Application.”
What is the front Controller in spring?
DispatcherServlet is called frontController in spring
ActionServlet is called FrontController in struts
What is DispatcherServlet?OR How DispatcerServlet works?
It is a FrontController any request must be forwared to DispatcherServlet.
This DS uses HandlerMapping to identify the our controller classes it will excute the control logic, controller will return the ModelAndView.
ModelAndView will identify view class using viewResolver. based on the MAView it will identify the jsp and finally jsp uses model to display the data
What is HandlerMapping?
It is used to identify the our controller classes
What is View Reslover?
It is used to identify the view classes and view class contains actual forwarding logic
What is the purpose of controller?
Controller are used to interact with service and dao layers
What are the controllers in spring?
We have the following controllers in spring
1. Controller
2. AbstarctController
3. MultiActionController
4. BaseCommandController
5. AbstractCommandController
6. AbstractFormController
7. SimpleFormController
8. AbstractWizardFormController
What is MutiActionController?
If there are many similar actions then we need to write many controllers .instead of writing many controllers we can use MutiActionController we can combine many similar actions into a single controller class with many methods
Ex.
Public class StudentController extends MultiActionController
{
Public ModelAndView insert(req,res,Student student)throws Exception
{
Sop(“insert()”);
Sop(“studentNo:”+student.getStudentNo());
return new ModelAndView(“success”);
}
Like do updateStudent()
}
What is BaseCommandController and AbsatractCommandController?
BaseCommandController
It is abstract base class.it has no more specific implementation like dataBinding,validation logics it doesn’t overrides the hadleRequestInternal() methods and no workflow
If we want to work with BaseCommandController we need to write the code for data population and validation
AbstractCommandController
It is also abstract it provides the work flow for BaseCommandController means overriding the handleRequestInternal ()
It is calling the getCommand()--------to create the command object
It is calling the bindValidate ()----------to populate() and validate the data
Explain Spring MVC framework with an example ?
The MVC is a standard software architecture that aims to separate business logic from presentation logic, enabling the development, testing and maintenance of both isolated.
The user triggers an event through the UI (click a button on the page or something).
(2) The controller receives the event and coordinates how things will happen on the server side, i.e. the flow goes to the objects required to perform the business rule..
(2) The controller receives the event and coordinates how things will happen on the server side, i.e. the flow goes to the objects required to perform the business rule..
To see Spring MVC hello world example
What is the default HandelMapping and what is the default View Reslover in spring MVC?
In spring mvc we are not giving any handlerMapping spring will take by default BeanNameUrlHandlerMapping
In spring mvc we are not giving any view resolver spring will take by default InternalResourceViewReslover
Where and How to implement validation in spring?
In spring ,we need implement the validation logic in separate class.
Write a class implement validator interface and override the supports() and validate() methods
Where and How to implement validation in struts?
In struts ,validation is implemented in form classes.
Write a form class extend ActionForm and override validate() method(programmatic validation)
What is HandlerIntercepter?
It is used to invoke pre and post logic for controller.The advantage of this is we can write it once and we can apply to N number of controllers
If we want to work with HandlerIntercepter we need override the preHandle() and postHandle(),afterCompletion() methods
How to handle Global Exception in spring mvc?
We need to use SimpleMappingExceptionReslover
If controller is not handling any Exception properly with try and catch blocks then we we will get 500 Error in the browser,user can not understand this message
If we are configuring SimpleMappingExceptionResolver then if controller is missing any Exception still we can display proper error message to the user
What is Controller in Spring MVC framework?
In a general way, Controller (‘c’ in mvc ) delivers access to the behavior of application which is usually defined by a service interface and acts as glue between core application and the web. It processes/interprets client data and..
What is a front controller in Spring MVC ?
A front controller is defined as “a controller which handles all requests for a Web Application.” DispatcherServlet (actually a servlet) is the front controller in Spring MVC that intercepts every request and then dispatches/forwards requests to an appropriate controller..
What is SimpleFormController in Spring MVC and how to use it in your web application ?
It offers you form submission support. This can help in modeling forms and populating them with model/command object returned by the controller. After filling the form, it binds the fields, validates the model/command object, and passes the object back to the controller so that the controller can take appropriate action..
What is AbstractController in Spring MVC ?
AbstractController : It provides a basic infrastructure and all of Spring’s Controller inherit from AbstractController. It offers caching support, setting of the mimetype etc. It has an abstract method ‘handleRequestInternal(HttpServletRequest, HttpServletResponse)’ which should be overridden by subclass..
What is ParameterizableViewController in Spring MVC ?
ParameterizableViewController : ParameterizableViewController is one of the concrete Spring’s Controller. This controller returns a view name specified in Spring configuration xml file thus eliminates the need of hard coding view name in the controller class as in case of AbstractController..
Why to override formBackingObject(HttpServletRequest request) method in Spring MVC?
You should override formBackingObject(HttpServletRequest request) if you want to provide view with model object data so that view can be initialized with some default values e.g. Consider in your form view..
What is the use of overriding referenceData(HttpServletReques) in Spring MVC ?
Overridden referenceData() method is used to provide some additional information to the formView (fillForm.jsp) to construct and display the view e.g.
In your form view you want to give various options to user say in the form of checkboxes and user can select multiple options. For this You can create multiple checkboxes in your view and corresponding multiple instance variables in your command/model object..
In your form view you want to give various options to user say in the form of checkboxes and user can select multiple options. For this You can create multiple checkboxes in your view and corresponding multiple instance variables in your command/model object..
When and how to use MultiActionController in Spring MVC ?
MultiActionController supports the aggregation of multiple request-handling methods into one controller, so allows you to group related functionality together. It is capable of mapping requests to method names and then invoking the correct method to handle a particular request…
Which are the different method name resolvers in MultiActionController ?
MultiActionController needs some way to resolve which method to call when handling an incoming http request. Spring provides MethodNameResolver interface to achieve this. The MultiActionController class provides a property named ‘methodNameResolver’ so that you can inject a MethodNameResolver…
No comments:
Post a Comment