Spring MVC principles and basic instance literacy
Recently, SpringMVC was first introduced in the project. Let's write down the things we 've seen and practiced over the past few days.
I. First, let's take a look at the framework of SpringMVC?
Spring Web MVC is a lightweight Web framework that implements the Web MVC Design Pattern Based on Java. It uses the MVC Architecture Pattern to decouple the web layer's responsibilities, request-driven refers to the use of the Request-response model. The purpose of the framework is to help us simplify development.
Ii. SpringMVCWorking principle:
1. Submit the client request to DispatcherServlet
2. The DispatcherServlet Controller queries one or more HandlerMapping and finds the Controller that processes the request.
3. The DispatcherServlet submits the request to the Controller.
4. After the Controller calls business logic processing, it returns ModelAndView
5. The DispatcherServlet queries one or more ViewResoler view Resolvers and finds the view specified by ModelAndView.
6. The view is responsible for displaying the results to the client.
3. After learning about springmvc's working principles, let's build it on our own:
1. Add the jar package on which springmvc depends to the created maven project:
org.springframework
spring-webmvc
3.2.8.RELEASE
org.springframework
spring-beans
3.2.8.RELEASE
org.springframework
spring-context
3.2.8.RELEASE
org.springframework
spring-aop
3.2.8.RELEASE
org.springframework
spring-jms
3.2.8.RELEASE
org.springframework
spring-context-support
3.2.8.RELEASE
org.springframework
spring-test
3.2.8.RELEASE
org.springframework
spring-jdbc
3.2.8.RELEASE
org.springframework
spring-web
3.2.8.RELEASE
org.springframework
spring-core
3.2.8.RELEASE
2. Configure web. xml and the front-end controller DispatcherServlet
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/dispatcher-servlet.xml
1
dispatcher
/
3. Create the springmvc configuration file under web-inf, [servletname]-servlet. xml
In the preceding configuration, the file name must be dispatcher-servlet. xml.
Text/plain; charset = UTF-8
4. Configure HandlerMapping to find the corresponding Conntroller Based on benaname (which can be omitted)
5. Create a JSP page that requires a request
6. Create your own Controller and inherit AbstractController to override handleRequestInternal
Here, we will learn how to transfer and receive page parameters.
Request Parameters passed in Controller:
1) First, 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 field
@RequestMapping(value=/add2.do)public String add(String userNumber,Integer passWord){System.out.println(userNumber:+userNumber+ passWord:+passWord);return user_add2;}In this way, if the page field is inconsistent with the parameter field, you can use @ RequestParam to set the alias. For example, if the page field is number and the actual parameter is userNumber, see the following code:
@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 method requires that the form field be consistent with the pojo field. If there is a check box on the page, the pojo class defines the array.
@RequestMapping(value=/add3.do)public String add3(UserInfo user){System.out.println(user);return user_add3;}The difference between strut2 and strut2 is that if strut2 is used on a page similar to user. userId, but does not need spring, it will not get the value if it is added.
Return data to the page:
1) The first method uses the request object setAttribute or 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) ModelAndView
@ RequestMapping (value =/add2.do) public ModelAndView add2 () {ModelAndView mav = new ModelAndView (user_add2); mav. addObject (userId, 1001); mav. addObject (userName, zhangsan); return mav ;}
3) The third method uses the Model object
@ RequestMapping (value =/add3.do) public String add3 (Model model) {model. addAttribute (personId, 9527); model. addAttribute (personName, Tang bohu); return user_add3 ;}
4) The fourth method is to use 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, Tang bohu); return user_add4 ;}FAQ: How can I return a json object?
First configure in springmvc-servlet.xml:
Then, inject ResponseBody in the controller to return the returned object to the page as json.
@ RequestMapping (/ajaxUser2.do) public @ ResponseBody UserInfo ajaxUser (Integer userId) {System. out. println (No.: + userId); UserInfo user = new UserInfo (); user. setUserId (userId); user. setUserName (zhangsan); user. setUserAge (18); return user ;}