Spring MVC Execution Process
Client's request submitted to Dispatcherservlet
Dispatcherservlet query one or more handlermapping, find the requested controller
Dispatcherservlet submits the request to the controller and returns Modelandview when the controller call is complete
Dispatcherservlet queries one or more corresponding front-end controllers to pass Modelandview to the specified front-end controller
Returns the rendered view to the client
The controller for SPRINGMVC is a singleton, as far as possible not to set properties, but in the method
@RequestMapping annotations are used above the class to represent the parent path of the class method
The main difference between SPRINGMVC and Struts2
SPRINGMVC's front controller is a servlet and struts is the filter
The SPRINGMVC controller is a singleton, while the STRUTS2 action is a multi-instance
SPRINGMVC is by parsing the contents of the request into formal parameters, encapsulating the corresponding and the pages as Modelandview objects, and struts2 using the value stack to store the request and the corresponding
SPRINGMVC's Interceptor
Class implements interface Handlerinterceptor, implementation methods, methods corresponding to the method before execution, after the page rendering
Then configure the interceptor in the configuration file
When multiple interceptors are executed together, the Prehandler is executed in the configured order, and the remaining two interceptors will be executed when Prehandler returns to true.
Posthandler and aftercompletion are called in reverse order of configuration
Json
Uploading files
Three major components and cores
Core: Dispatcherservlet (Front Controller)
Three main components:
Viewresolver: View resolver ()
Handlermapping: Processor Mapper Component (find handler for pathname)
Handleradapter: Processor Adapter Component (execution handler)
The main annotations
Interception rules
The return value of the controller
Modelandview: Returning views and parameters
Model: Pass model in method, return value to string, direct return view
Void: Nothing is reversed back
Redirect and request forwarding
- Add a keyword
return ‘redirect:/item/itemlist.action‘
to the front of the pathreturn ‘forward:/item/itemlist.action‘
Parameter binding
public ModelAndView toEdit(Integer id,HttpServletRequest request,HttpServletResponse response,HttpSession session)
Exception handling
Inherit handlerexceptionresolver implement the corresponding method, handle the error
SPRINGMVC Configuration<bean class="com.lyh.exception.CustomerException"></bean>
The "Type=test" string is included in the Intercept submission parameter
可以在@RequestMapping注解里面加上params="type=test"
Springmvc what objects are used to pass data from the background to the foreground
- Through the Modelmap object, you can put the object inside the object with the Put method, the foreground can be obtained by El Expression
How to put the data inside the Modelmap into the session
- You can add a @sessionattributes annotation to a class that contains a string that is the key to be placed inside the session
SPRINGMVC Controller is not a singleton mode, if yes, what is the problem, how to solve
- is a singleton mode, so in multi-threaded access when the thread security issues, do not use synchronization, will affect performance, the solution is in the controller can not write segments
Spring MVC interview Finishing