Introduction
-
What is springmvc?
Springmvc is a web framework based on mvc. It is a module of the spring framework. Both do not need to be integrated through intermediate integration. -
Execution steps of Springmvc framework
Step 1: The user initiates a request to the front-end controller (DispatcherServlet)
Step 2: Front-end controller requests processor mapper to find Handler
Step 3: Processor mapper returns Handler to the front-end controller
Step 4: The front-end controller calls the processor adapter to execute the Handler
Step 5: Processor adapter to execute Handler
Step 6: Handler execution is completed and returns ModelAndView to the adapter
Step 7: Processor adapter returns ModelAndView to Front-end Controller
ModelAndView is an underlying object of the springmvc framework, including Model and View
Step 8: The front-end controller requests the view parser to perform view parsing and parse it into a real view according to the logical graph name (jsp)
Step 9: View parser returns View to Front-end Controller
Step 10: Front-end controller performs view rendering, view rendering fills model data (in ModelAndView object) into the request field
Step 11: Front-end controller responds to the user -
Common annotations for Springmvc framework
@Controller
@Controller is used to mark a class, and the class marked with it is a springmvc Controller object. The distribution processor will scan the method that uses the annotation and check whether the method uses the @RequestMapping annotation.
@RequestMapping
This annotation is an annotation used to handle requested address mappings and can be used on classes or methods. For use on a class, all methods that represent that response requests in a class take this address as the parent path.
@Resource and @Autowired
@Resource and @Autowired are both used when injecting beans. In fact, @Resource is not an annotation for Spring. Its package is required to be imported, but spring supports the injection of this annotation.
Common points:
Both can be written on the field and setter methods. If both are written on fields, then there is no need to write the setter method again.
Differences:
@Autowired is an annotation provided by spring. @Autowired annotation is to assemble dependent objects according to type (byType). By default, it requires that dependent objects must exist. If null values are allowed, its required attribute can be set to false.
@Resource is automatically injected according to byName by default. @Resource has two important properties: name and type. Spring parses the name attribute annotated by @Resource to the name of the bean, and the type attribute is parsed into the type of the bean. So, if you use the name attribute, you use the automatic injection policy of byName, and when you use the type attribute, you use the automatic injection policy of bytype. If neither name nor type attribute is specified, the byName automatic injection strategy will be used through the reflection mechanism.
@ModelAttribute and @SessionAttributes
It represents: before calling all methods of this Controller, execute this @ModelAttribute method, which can be used in annotation and method parameters. This @ModelAttribute feature can be applied to BaseController. All Controllers inherit BaseController and can be implemented in the call
@PathVariable
Used to map template variables in the request URL to the parameters of the function processing method, that is, to take out the variables in the uri template as parameters.
@RequestParam
It is mainly used to obtain parameters in the springmvc background control layer, similar to ("name"), it has three common parameters: defaultValue="0", required=false, value="isApp"; defaultValue means setting the default value
@ResponseBody
This annotation is used to write the object returned by the Controller method to the body data area of the Response object after converting it to the appropriate HttpMessageConverter to the specified format
Time to use: The returned data is not a page with the html tag, but is used when data in some other format (such as json, xml, etc.);
@Component
It is equivalent to a general annotation, used when you don't know which layer some classes belong to, but it is not recommended.
@Repository
Used to annotate the dao layer, annotate it on the daoImpl class -
How to set redirection and forwarding in Springmvc?
Forward: Add "forward:" before the return value to make the result forward, such as "forward:?name=method
”
Redirect: Adding "redirect:" before the return value can redirect the return value, such as "redirect:
” -
Which annotation is generally used for controllers in Springmvc? Are there any other annotations that can be replaced?
-
Generally, @Controller annotation means that it is a control layer and cannot be replaced by other annotations.
-
Is Springmvc's controller singleton mode? If so, what are the problems and how to solve them?
It is singleton mode, so when accessing multithreads, it is also a thread safety issue. Don’t use synchronization, which will affect performance. The solution is that fields cannot be written in the controller. -
What is the use of @RequestMapping annotation on classes?
is an annotation used to handle request address mappings and can be used on classes or methods. Used on a class, it means that all methods in the class respond to requests take this address as the parent path. -
If there are many parameters passed in in the foreground and these parameters are all an object, how can I get this object quickly?
If you declare this object directly in the method, springmvc will automatically assign attributes to this object. -
SpringmvcThere is a class that combines the views and data together,What's the name? Called ModelAndView.
-
When a method returns a special object to AJAX, such as Object, List, etc., what needs to be done?
Add @ResponseBody annotation. -
Exception handling of Sprigmvc
Exceptions can be thrown to the Spring framework, and there is a Spring framework to handle them; we only need to configure a simple exception handler and add a view page to the exception handler.
The benefits of SpringMVC
Advantages of Springmvc
(1) Provide a DispatcherServlet in Spring MVC without additional development.
(2) Use xml-based configuration files in springMVC, which can be edited without recompiling the application.
(3) springMVC instantiates the controller and constructs beans based on user input.
(4) springMVC can automatically bind user input and convert data types correctly. For example, springMVC can automatically parse strings and set properties of type float or decimal.
(5) springMVC can verify user input. If the verification fails, it will be redirected back to the input form. Input verification is optional, and supports encoding methods and declarations. Regarding this, springMVC has common validators built in.
(6) springMVC is part of the spring framework. Other capabilities provided by spring can be utilized.
(7) springMVC supports internationalization and localization, and supports displaying multi-language languages according to user area.
(8) springMVC supports a variety of view technologies, the most common JSP technology and other technologies include Velocity and FreeMarker
Detailed explanation:
- Clear role division: controller, validator, command object, form object, model object, Servlet distributor (DispatcherServlet), processor mapping, attempt parser (view resolver), etc. Each role can be implemented by a special object.
- Powerful and direct configuration method: Both framework classes and application classes can be configured as JavaBeans, supporting references across multiple contexts, such as references to business objects and validator validator) in web controllers.
- Adaptable and non-invasive: You can choose the controller subclass (simple type, command type, from type, wizard type, multi-action type or custom) according to different application scenarios, rather than inheriting it by a single controller (such as Action/ActionForm).
- Reusable business code: You can use existing business objects as commands or form objects without extending the base class of a specific framework.
- Customizable binding and validation: For example, using type mismatch as an application-level verification error, which can guarantee the wrong value. For example, localized date and number binding, etc. In some other frameworks, you can only use string form objects, you need to manually parse it and convert it to a business object.
- Customizable handler mapping and view resolution: Spring provides from the simplest URL mapping to complex, dedicated customization strategies. Spring appears more flexible than some web MVC frameworks that force developers to use a single specific technology.
- Flexible model conversion: In the Spring web framework, use Map-based key/value pairs to achieve easy integration with various view technologies.
- Customizable localization and theme parsing: supports optional use of Spring tag libraries in JSP, supports JSTL, supports Velocity (no additional intermediate layers are required), etc.
- Simple and powerful JSP tag library: supports many features such as data binding and theme. He offers maximum flexibility in marking.
- JSP form tag library: The form tag library introduced in Spring 2.0 is easier to write forms in JSP.
- The life cycle of a Spring Bean can be limited to the current HTTP Request or HTTP Session. To be precise, this is not a feature of the Spring MVC framework itself, but should be attributed to the WebApplicationContext container used by Spring MVC.
Specific analysis of SSM
Advantages of SSM three frameworks.
-
1. Pros and cons of mybatis:
Advantages: a. SQL is written in an XML file, which facilitates unified management and optimization, and uncoupling between SQL and program code.
b. Provide mapping labels, support mapping of object and database orm field relationships, support object relationship mapping labels, and support the establishment of object relationships
c. Provide XML tags and support writing dynamic SQL.
Disadvantages: a. The workload is large, especially when there are many fields in the table and many related tables.
b. The writing of SQL statements depends on the database and has poor portability.
c. Cascading deletion is not supported. Cascading updates are required, and the table needs to be deleted by yourself. -
2. Advantages of spring:
A. Through Spring's IOC characteristics, the dependencies between objects are handed over to Spring control, which facilitates decoupling and simplifies development.
B. Through Spring's AOP feature, it is easy to control transactions, logs, and permissions.
C. Provides integrated support for other excellent open source frameworks.
D. Low intrusive. -
3. Advantages of SpringMVC
A. springMVC is a lightweight web framework that uses the MVC design idea to decouple the web layer. Yes, our development is simpler.
B. Seamlessly connect with Spring.
C. Flexible data verification, formatting, and data binding mechanism. -
4. Integrated Analysis
A. spring MVC + spring + mybatis is a standard MVC design model, which divides the entire system into four layers: display layer, controller layer, service layer, and DAO layer.
Use Spring MVC to be responsible for forwarding and view management of requests
Spring implements business object management, and mybatis serves as the persistence engine of data objects.
B. Spring is an open source framework. Spring is a lightweight inversion of control (IoC) and tangent-oriented (AOP) container framework, which can better integrate other frameworks.
C. Spring MVC framework has an MVC framework that separates data, business and presentation well by implementing the Model-View-Controller model.
D. MyBatis is a Java-based persistence layer framework