Model
The last article, "Spring6: Spring MVC based on Annotations," describes spring MVC environment building, @RequestMapping, and parameter binding, which is the most basic and important part of spring MVC, and this article goes on to talk about spring The rest of the knowledge in MVC begins with the model.
The previous article in a more detailed interpretation of the data from the page request to the server backstage some details, then the next problem is how the data from the backstage back to the foreground, the answer is here to say the model, about the model before writing an example, I specifically explained three points:
1, the model itself is an interface, its implementation class for Extendedmodelmap, in addition to using the model can also use Modelandview, Modelmap these, but if there is no special needs, the use of model is relatively simple, Personally, I prefer to use model
2,the model's life cycle is request, that is, to pass the model value can only use forwarding and cannot use the redirection
3, why use model instead of request, the main reason is to reduce the code of the intrusion or the coupling of the code is also OK . Because model is a spring component, request is a component of the Java EE, and using model instead of request can reduce the reliance on the Java EE and facilitate debugging
OK, next look at the example, the overall code or according to the previous article, first look at the background code:
= "/test")publicclass testcontroller{ @RequestMapping public String dispatchtest (test test, model) { Model.addattribute ("Modelkey", "Modelvalue" ); return "Test";} }
Just plug a key-value into the model, and then forward to test.jsp, test.jsp page to take the model value, can be obtained by Jstl (El Expression can also), anyway, directly on the JSP page through the "<% ...%>" Writing Java scripts is not feasible. The test.jsp page reads this:
<%@ Page Language="Java"Import="java.util.*"pageencoding="iso-8859-1"%><%@ taglib URI="Http://java.sun.com/jsp/jstl/core"prefix="C" %><%@ taglib URI="http://java.sun.com/jsp/jstl/fmt"prefix="FMT" %><%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <Basehref= "<%=basePath%>"> <title>Test page</title> <Metahttp-equiv= "Pragma"content= "No-cache"> <Metahttp-equiv= "Cache-control"content= "No-cache"> <Metahttp-equiv= "Expires"content= "0"> <Metahttp-equiv= "keywords"content= "Keyword1,keyword2,keyword3"> <Metahttp-equiv= "description"content= "This is my page"> <!--<link rel= "stylesheet" type= "Text/css " href= "Styles.css" > - </Head> <Body> <C:outvalue= "${modelkey}" /> </Body></HTML>
OK, then visit the " http://localhost:8080/SpringMVC/test "This address, on the page" Modelvalue "These characters will come out.
Previously said, the model of the life cycle is the request, then if the page is redirected to test.jsp above, it is certainly not get "modelvalue", you can try it yourself, so redirect the past, you have to set the data in the background to the session.
test.jsp page does not change, controller can change this:
= "/test")publicclass testcontroller{ @RequestMapping public String dispatchtest (test test, HttpSession session) { Session.setattribute ("Modelkey", " Modelvalue "); return "Redirect:/test.jsp"; // return "test"; }}
Can try, and then visit the "http://localhost:8080/SpringMVC/test" this address, "Modelvalue" These characters on the page out.
In spring MVC, the Request, Response, Session, InputStream, OutputStream objects are automatically injected, but as previously stated, in order to reduce the intrusion and coupling of code, It's good to not use as much as possible or not to use these Java EE objects.
Interceptors (Interceptor)
The interceptor in SPRINGMVC is the same as the filter in the Java EE, it is very important and useful, its main function is to intercept the user's request and do the corresponding processing, such as through it to verify the permissions, or to determine whether the user login.
The method of using interceptors in SPRINGMVC is relatively simple, first implements Handlerinterceptor interface, realizes Aftercompletion, Posthandle, prehandle three abstract methods, This defines two interceptor:
Public classTestInterceptor1Implementshandlerinterceptor{ Public voidaftercompletion (httpservletrequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throwsException {System.out.println ("Testinterceptor1.aftercompletion ()"); } Public voidPosthandle (httpservletrequest arg0, HttpServletResponse arg1, Object arg2, Modelandview arg3)throwsException {System.out.println ("Testinterceptor1.posthandle ()"); } Public BooleanPrehandle (httpservletrequest arg0, HttpServletResponse arg1, Object arg2)throwsException {System.out.println ("Testinterceptor1.prehandle ()"); return true; }}
Public classTestInterceptor2Implementshandlerinterceptor{ Public voidaftercompletion (httpservletrequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throwsException {System.out.println ("Testinterceptor2.aftercompletion ()"); } Public voidPosthandle (httpservletrequest arg0, HttpServletResponse arg1, Object arg2, Modelandview arg3)throwsException {System.out.println ("Testinterceptor2.posthandle ()"); } Public BooleanPrehandle (httpservletrequest arg0, HttpServletResponse arg1, Object arg2)throwsException {System.out.println ("Testinterceptor2.prehandle ()"); return true; }}
Explain the effect of the three methods:
1. Aftercompletion: Executes the contents of the method after the entire view has been rendered, mainly for releasing some resources
2, Posthandle: After the controller executes, the view renders the contents of the method before execution, that is, the Posthandle method can manipulate the model
3, Prehandle: Before the controller executes, the contents of the method is executed, note that the method has a return value, and when the method returns false, the entire request ends.
Then add the interceptor configuration to the Springmvc-servlet.xml:
<!--Configuring Interceptors -<mvc:interceptors> <Mvc:interceptor> <mvc:mappingPath= "/test" /> <Beanclass= "Com.xrq.interceptor.TestInterceptor2" /> </Mvc:interceptor> <Mvc:interceptor> <mvc:mappingPath= "/test" /> <Beanclass= "Com.xrq.interceptor.TestInterceptor1" /> </Mvc:interceptor></mvc:interceptors>
If there are multiple interceptors, the order in which the "<mvc:interceptor>...</mvc:interceptor>" is defined is the order in which the interceptors are executed.
The following continues to access "Http://localhost:8080/SpringMVC/test", and the result of the code execution is:
Testinterceptor2.prehandle () Testinterceptor1.prehandle () Testinterceptor1.posthandle () Testinterceptor2.posthandle () testinterceptor1.aftercompletion () testinterceptor2.aftercompletion ()
Maybe some friends do not understand the result of this implementation, I actually understand, but it is not clear at a sudden.
If you are not a very understanding of friends, you can look at the Java design pattern inside the responsibility chain mode, the interceptor call method is actually a kind of chain call method, TestInterceptor2 call TestInterceptor1, The TestInterceptor1 method will go back to the TestInterceptor2 method.
Spring7: annotation-based spring MVC (next)