springmvc most authoritative knowledge

1. What is Spring MVC? A brief introduction of your understanding of springMVC?

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, AOP, etc.);

(3) a clear role assignment: front-end controller (dispatcherServlet), maps the request to the 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) the DispatcherServlet front controller (not require programmers to develop)

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 to develop)

Role: According to the URL request to find Handler

(3) Processor Adapter HandlerAdapter

Note: To follow the rules HandlerAdapter required to write at the time of writing Handler, so that we can correct adapter HandlerAdapter to perform Handler.

(4) Processor Handler (requires programmers to develop)

(5) ViewResolver view resolver (not require programmers to develop)

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, the implementation of which support different types of views (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 value preceded by "forward:", such as "forward: user.do name = method4?"

(2) redirect: returning the value preceded by "redirect:", such as "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) Configuration json mapping in the configuration file

(3) may return Object, List et Ajax receiving method which directly, but to add the foregoing method @ResponseBody comment.

 

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

(1) post requests to solve the garbage problem:

A filter disposed in web.xml CharacterEncodingFilter, 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 the request parameters Chinese garbled solution appears there are two:

① modify tomcat configuration file to add the same coding and engineering codes, as follows:

<ConnectorURIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

 ② Another method of re-encoding parameters:

String userName = new String(request.getParamter("userName").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 @Controller with annotations, you can use @ RestController, @ RestController notes equivalent @ResponseBody + @Controller, representation is the presentation layer, in addition, other generally do not comment instead.

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, you will end up calling the invoke method 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/aibabel/p/11688442.html