SpringMVC knowledge combing - Interview essential

1. What is Spring MVC?

Spring MVC is a Java-based implementation of the request for driving the MVC design pattern type lightweight Web framework, the web layer functions to decouple the Model, View, Controller separation, the complex web application divided into several logic clear part, to simplify development, reduce error, the fit between the easy to develop group.

2, SpringMVC process?

(1) The user sends a request to the front-end controller the DispatcherServlet;

(2) the DispatcherServlet after receiving the request, the call processor HandlerMapping mapper request to obtain the Handle;

(. 3) the processor finds particular mapping processor generation processing according to the request url blocker object and a processor (if it is generated) collectively returned to the DispatcherServlet;

(. 4) to call the DispatcherServlet HandlerAdapter processor adapter;

(. 5) adapted through HandlerAdapter specific call processor (Handler, also called back-end) ;

(. 6) perform complete Handler returns ModelAndView;

(. 7) Handler HandlerAdapter is the execution result is returned to ModelAndView the DispatcherServlet;

(. 8) to the DispatcherServlet ModelAndView pass parser parses ViewResolver view;

return after DETAILED view (. 9) analytical ViewResolver;

(10 ) view of the DispatcherServlet render view (model data coming filled view)

(. 11) in response to the DispatcherServlet user.

3, Springmvc advantages:

(1) may support various view technologies, not just the JSP;

(2) integrated with the Spring framework (e.g. IoC container, the AOP, etc.);

(3) a clear role assignment: front-end controller (DispatcherServlet), to request mapping processor (handlerMapping), the adapter processor (HandlerAdapter), view resolver (ViewResolver).

(4) supports a variety of mapping policies of the requested resource.

4, Spring MVC's major components?

(1) DispatcherServlet front controller (not require programmers)

action: receiving a request, a response result, the equivalent of the transponder, with DispatcherServlet reduces the coupling between the other components.

(2) the HandlerMapping mapper processor (not require programmers)

effect: The request URL to locate Handler

(. 3) Processor Adapter HandlerAdapter

NOTE: To HandlerAdapter accordance with the rules required to write in the preparation of the Handler, so that the adapter HandlerAdapter before you can go right to perform Handler.

(4) Handler Processor (programmers needed)

(5) ViewResolver view resolver (not require programmers)

action: parsing view, a view of the logical name resolves to a real view (View)

(. 6) View View (requires programmers to develop JSP)

view is an interface, its implementation class support different view types (jsp, freemarker, pdf, etc.)

5, the difference springMVC and struts2 What?

(1) springmvc i.e. inlet is a front controller servlet (DispatchServlet), and a filter inlet struts2 worrying too much (
StrutsPrepareAndExecuteFilter).

(2) springmvc method is based on the development of (a method corresponding to a url), request parameters passed to the method parameter, or can be designed as a single Example Example (recommended singleton), Struts2 is class-based development, the parameter is passed through the class the property can only be designed to be more cases.

(3) Struts using the data value stack memory requests and responses through OGNL access data, the parser is a parameter SpringMVC REQUEST request content analysis, and a parameter assignment method, and views into the data package ModelAndView object, and finally the model data is transferred to the page ModelAndView by reques domain. Jsp view parser jstl default.

6, SpringMVC how to set redirection and forwarding?

(1) Forward: the return to the previous value plus "forward:", such as "Forward: user.do?name=method4"

(2) Redirection: In return to the previous value plus "redirect:", such as "
the redirect: HTTP: / /www.baidu.com "

7, SpringMvc how AJAX and call each other?

Jackson by the frame can be put inside the Java objects directly into Json Js identifiable objects. Specific steps are as follows:

(1) was added Jackson.jar

(2) arranged json mapping in the configuration file

(3) Ajax receiving method which can directly return Object, List, etc., but the front @ResponseBody To add annotations.

8, how to solve the problem of Chinese garbled POST request, GET is how to deal with it?

(1) to solve the problem of distortion post request:

Configure a CharacterEncodingFilter filter in web.xml arranged utf-8;

<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>

  



(2) get request Chinese distortion parameter has two solutions:

① adding modify tomcat profile consistent coding and coding Engineering follows:

<ConnectorURIEncoding = "UTF-. 8" connectionTimeout = "20000" Port = "8080" = Protocol " the HTTP / 1.1 "the redirectPort =" 8443 "/>

② another method of re-encoding the parameters:

String = the userName new new String (request.getParamter (" the userName. ") the getBytes (" ISO8859-1 ")," UTF-. 8 ")

ISO8859-1 is tomcat default encoding, content needs to be encoded by the tomcat utf-8 encoding.

9, Spring MVC exception handling?

A: You can throw an exception Spring framework, is handled by the Spring framework; we only need to configure a simple exception handler, Tim can view the page in the exception handler.

10, SpringMvc controller is not a singleton, and if so, what is the problem, how to solve?

A: Yes, a singleton, so when in multithreaded access have thread-safety issues, do not use synchronization, will affect the performance of the solution is not written in a field controllers inside.

11, SpringMVC commonly used annotation what?

@RequestMapping: url mapping for processing a request for comments, and can be used for class or method. For the class, in response to the request by any methods that are based on the class as a parent of the path address.

@RequestBody: annotation data received http request json implemented will convert json java object.

@ResponseBody: Notes achieve conreoller method returns the object into json response to customer objects.

12, notes the controller SpingMvc in general with that, there is no other alternative to comment?

A: Generally used @Conntroller annotation indicating that the presentation layer, can not be replaced with other comments.

13, if the intercept request, I would like to get intercepting method of submission, how to configure?

A: You can add notes inside method = RequestMethod.GET in @RequestMapping.

14, in which the method how to get Request, or Session?

A: The statement request directly in the parameter method, SpringMvc automatically passed to the request object.

15, from the front desk if you want to get the parameters passed in a method to intercept it, how to get?

A: Direct declare this parameter in parameter inside it, but the name and parameters must pass over the same.

16, the front desk if there are a number of parameters passed, and these are the parameters of an object, then how to quickly get this object?


A: Direct declare this object in a method, SpringMvc property will automatically assign to this object inside.

17, what is the return value of the function is SpringMvc?

A: The return value can have many types, there String, ModelAndView. ModelAndView the view class and data are merged together, but generally the String better.

18, SpringMvc what the object from the front desk to pass data back?

A: By ModelMap object, you can call the put method in which the object, the object is added to the inside, the front desk you can get by el expressions.

19, how to put data into the Session ModelMap inside there?

A: You can add @SessionAttributes annotation, which contains the string is to be placed inside the session key in a class above.

20, SpringMvc inside the interceptor is how to write:

There are two ways for writing, one is to achieve HandlerInterceptor interface adapter class inherits another, followed by interface method among the processing logic implemented; then disposed in the interceptor can SpringMvc profile:
<! - Configure SpringMvc interceptor -> 

<MVC: interceptors> 

<! - Configure a blocker Bean on it by default to all requests are intercepted -> 

<bean the above mentioned id = "myInterceptor" class = " com.zwp.action.MyHandlerInterceptor "> </ the bean> 

<- intercept only for part of the request ->! 

<MVC: interceptor> 

<MVC: Mapping path =" / modelMap.do "/> 

<the bean class =" COM .zwp.action.MyHandlerInterceptorAdapter "/> 

</ MVC: Interceptor> 

</ MVC: interceptors>

  


21, notes principle:

Notes is essentially a special interface Annotation inherited, the specific implementation class is generated by the Java run-time dynamic proxy class. When we get notes through reflection, returns generated Java run-time dynamic proxy object. Custom annotation method calls through a proxy object, it will eventually call the
invoke method of AnnotationInvocationHandler. This method corresponding to an index value from this memberValues Map. The source memberValues is Java constant pool.

Guess you like

Origin www.cnblogs.com/xq-943746/p/10982885.html