Springmvc learning - basic knowledge assessment

Big a sixty five2022-06-23 18:06:02

SpringMvc Study --- Basic knowledge assessment



SpringMVC

1、SpringMVC workflow


technological process :

1、 User sends request to front controller DispatcherServlet
2、DispatcherServlet Received request call HandlerMapping Processor mapper .
3、 The processor mapper finds the specific processor , Generating processor objects and interceptors ( Generate if any ) Return to DispatcherServlet.
4、DispatcherServlet call HandlerAdapter Processor adapter
5、HandlerAdapter Call the specific processor through adaptation (Controller, Also called back end controller ).
6、Controller Execution complete return ModelAndView
7、HandlerAdapter take controller Execution results ModelAndView Return to DispatcherServlet
8、DispatcherServlet take ModelAndView Pass to ViewReslover view resolver
9、ViewReslover Return details after parsing View
10、DispatcherServlet according to View Render view ( Fill model data into view ).
11、DispatcherServlet Responding to users  


2、 How to solve POST Request Chinese garbled code problem ,GET How to deal with it ?

stay web.xml Add :
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The above can be solved post Ask for random code . about get There are two ways to solve the problem :

modify tomcat The added code of configuration file is consistent with the project code , as follows :
<ConnectorURIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
Another way to recode parameters :
String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")
ISO8859-1 yes tomcat Default encoding , Need to put tomcat Press... After coding utf-8 code
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

3.SpringMVC And Struts2 The main difference ?


①springmvc The entrance is a servlet Front end controller , and struts2 The entrance is a filter filter .

②springmvc Method based development , Pass parameters through method parameters , It can be designed as a singleton or multiple cases ( Suggest the singleton ),struts2 It's class-based development , Passing parameters is through the properties of the class , Can only be designed for multiple cases .
③Struts A value stack is used to store the data for the request and response , adopt OGNL Access data , springmvc Through the parameter parser is will request Object content is parsed into method parameters , Encapsulate response data and pages into ModelAndView object , Finally, the model data is passed through request Objects are transferred to the page . Jsp View parser by default jstl.


 4. Talk a SpringMvc and Struts1,Struts2 The comparative advantage of


  On the performance Struts1>SpringMvc>Struts2 In terms of development speed SpringMvc and Struts2 almost , Than Struts1 higher


5. Talk a SpringMvc What is the core entry class of ,Struts1,Struts2 What are the differences between


  SpringMvc Yes. DispatchServlet,Struts1 Yes. ActionServlet,Struts2 Yes. StrutsPrepareAndExecuteFilter


6. SpringMvc Is the controller in singleton mode , If it is , What's the problem? , How to solve


  It's the singleton mode , So there are thread security issues when accessing multiple threads , Don't use sync , It will affect performance , The solution is that fields cannot be written in the controller


7. SpingMvc The controller annotation in is usually the , Is there any other annotation to replace


  It's usually used @Conntroller annotation , It means the presentation layer , You can't replace... With other annotations .


8. @RequestMapping What's the use of annotations on classes

  For classes , This address is the parent path of all methods in the class that respond to requests .

9. How to map a request to a specific method


  Annotate the method directly @RequestMapping, And write the path to intercept in this annotation


10. If in the intercept request , I want to intercept get Method of submission , How to configure


  Can be in @RequestMapping Add... To the note method=RequestMethod.GET


11. If in the intercept request , I want to block submission parameters that contain "type=test" character string , How to configure


  Can be in @RequestMapping Add... To the note params="type=test"


12. I want to get the parameters passed in from the front station in the interception method , How to get


  You can declare this parameter directly in the parameter , But the name must be the same as the parameter passed


13. If the foreground has many parameters passed in , And these parameters are all the parameters of an object , So how to get this object quickly


  Declare the object directly in the method ,SpringMvc It will automatically assign properties to this object


14. How to get... In the method Request, perhaps Session


  Declare... Directly in the parameter of the method request,SpringMvc Just put request Object to


15. SpringMvc What is the return value of the function in .


  There are many types of return values , Yes String, ModelAndView, When used in general String better


16. SpringMvc How to deal with the return value of


  SpringMvc According to the configuration file InternalResourceViewResolver Prefixes and suffixes for , Use prefix + Return value + Suffixes make up the complete return value


17. SpringMVC How to set redirection and forwarding


  Add... Before the return value "forward:" You can forward the results , for example "forward:user.do?name=method4" Add... Before the return value "redirect:" You can redirect the return value , for example "redirect:​ ​http://www.baidu.com"​


18. SpringMvc What object is used to transfer data from the background to the front desk


  adopt ModelMap object , It can be used in this object put Method , Add objects to it , You can go through the front desk el The expression gets


19. SpringMvc There's a class in that merges views and data , What is called?


  It's called ModelAndView


20. How to put ModelMap Put the data in Session Inside


  You can add... To the class @SessionAttributes annotation , The string contained in it is to put session Inside key


21. SpringMvc How and AJAX Calling each other


  adopt Jackson The frame can put Java The objects in it are directly transformed into Js Recognizable Json object
  The specific steps are as follows
  1. Join in Jackson.jar
  2. Configure in profile json Mapping
  3. In an interview with Ajax Method can directly return to Object,List etc. , But add... Before the method @ResponseBody annotation


22. When a way to AJAX Return special object , for example Object,List etc. , What needs to be done


  To add @ResponseBody annotation


23. SpringMvc How is the interceptor written inside


There are two ways of writing , One is to implement interfaces , The other is the inheritance adapter class , And then in SpringMvc The interceptor can be configured in the configuration file of :
<!-- To configure SpringMvc Interceptor -->
<mvc:interceptors>
<!-- Configure an interceptor Bean That's all right. The default is to block all requests -->
<bean id="myInterceptor" class="com.et.action.MyHandlerInterceptor"></bean>

<!-- Block only for partial requests -->
<mvc:interceptor>
<mvc:mapping path="/modelMap.do" />
<bean class="com.et.action.MyHandlerInterceptorAdapter" />
</mvc:interceptor>
</mvc:interceptors>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

24. Talk a SpringMvc The implementation process of



    When the system is started, it is created according to the configuration file spring The container of , The first is to send http Request to core controller disPatherServlet,spring The container uses the mapper to find the business controller ,
Use the adapter to find the corresponding business class , Data encapsulation when entering business class , Type conversion may be involved before encapsulation , Use... After executing the business class ModelAndView Forward the view , The data is in model in , use map Transfer data for page display .



author :​​ Small a ninety-seven ​​​ ​

-------------------------------------------

Individuality signature : Everything is good in the end , If it's not good , That means things haven't come to the end yet ~

The copyright of this article belongs to the author 【​​ Small a ninety-seven ​​​】, Welcome to reprint , However, this statement must be retained without the consent of the author , And in the article page obvious position gives the original link , Otherwise, the right to pursue legal responsibility is reserved !




thank
Similar articles