The recent project has just touched the SPRINGMVC, put these days to see and practice of things to write it out.
Firstly, first of all, what kind of framework is SPRINGMVC?
Spring Web MVC is a lightweight web framework that implements the request-driven type of the Web MVC design pattern based on Java, that is, the idea of using the MVC architecture pattern, decoupling the WEB layer from responsibility, and based on the request-driven approach is to use the request-response model, The purpose of the framework is to help us streamline development.
Second, the working principle of SPRINGMVC:
1.client requests are submitted toDispatcherservlet
2.byDispatcherservletThe controller queries one or morehandlermapping, locate the request that handles theController
3. Dispatcherservletsubmit the request toController
4. Controllerafter invoking business logic processing, returnsModelandview
5. Dispatcherservletquery one or moreViewresolerView parser, findModelandviewthe specified view
6.The view is responsible for displaying the results to the customerEnd
Thirdly, after understanding the working principle of SPRINGMVC, we will build it ourselves:
1. Add the jar packages that SPRINGMVC relies on to create a MAVEN project:
<span style= "White-space:pre" ></span><dependency><groupid>org.springframework</ groupid><artifactid>spring-webmvc</artifactid><version>3.2.8.release</version></ Dependency><dependency><groupid>org.springframework</groupid><artifactid> Spring-beans</artifactid><version>3.2.8.release</version></dependency><dependency ><groupId>org.springframework</groupId><artifactId>spring-context</artifactId>< Version>3.2.8.release</version></dependency><dependency><groupid> Org.springframework</groupid><artifactid>spring-aop</artifactid><version>3.2.8.release </version></dependency><dependency><groupId>org.springframework</groupId>< artifactid>spring-jms</artifactid><version>3.2.8.release</version></dependency>< Dependency><groupid>org.springframework</groupid><artifactid>spring-context-support</artifactid><version>3.2.8.release</ version></dependency><dependency><groupid>org.springframework</groupid>< Artifactid>spring-test</artifactid><version>3.2.8.release</version></dependency> <dependency><groupid>org.springframework</groupid><artifactid>spring-jdbc</ Artifactid><version>3.2.8.release</version></dependency><dependency><groupid> Org.springframework</groupid><artifactid>spring-web</artifactid><version>3.2.8.release </version></dependency><dependency><groupId>org.springframework</groupId>< Artifactid>spring-core</artifactid><version>3.2.8.release</version></dependency>
2. Configure the Web. XML to configure the front-segment controller Dispatcherservlet
<span style= "White-space:pre" ></span><servlet><servlet-name>dispatcher</servlet-name ><servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param ><param-name>contextconfiglocation</param-name><param-value>/web-inf/ Dispatcher-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup> </servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/ </url-pattern></servlet-mapping>
3. Create the SPRINGMVC configuration file under Web-inf,[Servletname]-servlet.xml
As the above configuration, the file name must be Dispatcher-servlet.xml
<!--default annotation mapping Support--<mvc:annotation-driven/> <!--automatically scanned package name--<context:component-scan Base-package= "Com.acxiom"/> <!--access to static resource files--><mvc:resources location= "/resources/" mapping= "/ resources/** "/><!--spring MVC distribution Request--><bean class= for @controllers" Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping "/><bean class=" Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/><!--View Explanation class--<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" ><property name= "prefix" value= "" ></property><property name= "suffix" value= ". JSP" ></property></bean><!--read from request and response Writing string--><bean id= "Stringhttpmessage" class= "Org.springframework.http.converter.StringHttpMessageConverter" ><property name= "Supportedmediatypes" ><list><value>text/plain;charset=UTF-8</value> </list></property></bean>
4. Configure handlermapping to find corresponding Conntroller according to Benaname (can be omitted)
<bean class= "Org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" ></be An>
5. Create a JSP page that requires a page to make a request
6. Create your own controller inheritance Abstractcontroller rewrite handlerequestinternal
Here, we focus on the next page to learn the parameters of the delivery and reception.
Pass request parameters in controller:
1) The first through the request object or Session object
@RequestMapping (value= "/add1.do") public String Add (HttpServletRequest request) {string Usernumber = Request.getparameter ("Usernumber"); String PassWord = Request.getparameter ("PassWord"); System.out.println ("Usernumber:" +usernumber+ "PassWord:" +password); return "USER_ADD1";}
2) The second parameter field is consistent with the page fields
<span style= "White-space:pre" ></span> @RequestMapping (value= "/add2.do") public string Add (string Usernumber,integer PassWord) {System.out.println ("<span style=" font-family:arial, Helvetica, Sans-serif; " >usernumber</span>: "+usernumber+" PassWord: "+password"; return "USER_ADD2";}
In this way, if the page field is inconsistent with the parameter fields, you can set the alias by @requestparam, such as number, the actual use of the parameter is Usernumber, see the following code:
<span style= "White-space:pre" ></span> @RequestMapping (value= "/add2.do") public String Add (@RequestParam (value= "number") String Usernumber,integer PassWord) {System.out.println ("Usernumber:" +usernumber+ "PassWord:" +password); return " User_add2 ";}
3) The third way of passing objects requires that the form fields be consistent with the Pojo class field, and if the page has a check box, the array is defined in the Pojo class.
@RequestMapping (value= "/add3.do") public String add3 (UserInfo user) {System.out.println (user); return "USER_ADD3";}
The difference with STRUT2 is that if the strut2 is to be used similar to User.userid on the page, but spring does not need to, the addition will not take the value.
Return Data to page:
1) The first through the Request object setattribute, you can also use the Session object
@RequestMapping (value= "/add1.do") public String Add (HttpServletRequest request) {string Usernumber = Request.getparameter ("Usernumber"); String PassWord = Request.getparameter ("PassWord"); System.out.println ("Usernumber:" +usernumber+ "PassWord:" +password); String str = "Usernumber:" +usernumber+ "PassWord:" +password;request.setattribute ("user", str); return "USER_ADD1";}
2) The second type of Modelandview
@RequestMapping (value= "/add2.do") public Modelandview add2 () {Modelandview Mav = new Modelandview ("User_add2"); Mav.addobject ("UserId", 1001), Mav.addobject ("UserName", "Zhang San"); return MAV;}
3) Third through the model object
@RequestMapping (value= "/add3.do") public String ADD3 (model model) {Model.addattribute ("personId", 9527); Model.addattribute ("PersonName", "Tong Pak Fu"); return "USER_ADD3";}
4) fourth through map
@RequestMapping (value= "/add4.do") public String add4 (Integer personid,string username,map Map) {System.out.println ( PERSONID); System.out.println (UserName); Map.put ("PersonId", 9527), Map.put ("PersonName", "Tong Pak Fu"); return "User_add4";}
FAQ: How do I implement a JSON object if I want to return it?
First configure in Springmvc-servlet.xml:
<!--used to convert an object to JSON--><bean id= "Jsonconverter" class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "></bean><bean class=" Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "><property name=" Messageconverters "><list><ref bean=" stringhttpmessage "/><ref bean=" JsonConverter "/></ List></property></bean>
Then inject responsebody in the controller to return the object as JSON back to the page
@RequestMapping ("/ajaxuser2.do") public @ResponseBody UserInfo Ajaxuser (Integer userId) {System.out.println ("Number:" + USERID); UserInfo user = new UserInfo () User.setuserid (userId) user.setusername ("Zhang San"); user.setuserage; return user;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring MVC principles and examples basic literacy articles