About the spring MVC mechanism, sample interpretation

Source: Internet
Author: User

Spring MVC separates the controller from the model object. The working process is as follows:

1.Spring MVC requests that all requests be submitted to Dispatcherservlet, which will delegate the other modules of the application system responsible for the actual processing of the request.
2.DispatcherServlet queries one or more handlermapping to find the controller that handles the request.
3.DispatcherServlet request submission to target controller.
4.Controller After the business logic is processed, a modelandview is returned.
5.Dispathcher queries One or more Viewresolver view resolvers to locate the view object specified by the Modelandview object.
6. The View object is responsible for rendering back to the client.

Springmvc Example Interpretation:

    helloworldcontroller code comments are as follows:

Package cn.training.controller;import org.springframework.beans.factory.annotation.autowired;import  org.springframework.stereotype.Controller;import org.springframework.ui.Model;import  org.springframework.web.bind.annotation.requestmapping;import  org.springframework.web.bind.annotation.requestmethod;import cn.training.userbean.userbean;import  cn.training.service.*;//found the class named Helloworldcontroller @controller ("Helloworldcontroller")//Access Path @requestmapping ("/ ")//helloworldcontroller class public class helloworldcontroller {   //Auto-inject label       @Autowired     //declares a helloservice variable     helloservice  helloservice;    //Request Access Path      @RequestMapping in Get mode (value =   "/",  method = requestmethod.get)     //Initlogin ( ) method with one parameter      public string initlogin (Model model) &NBsp {     //go to HelloWorld. JSP page         return  "HelloWorld";    }     //Access Path      @RequestMapping by post (value= "/init",   method= Requestmethod.post)     //with two parameters InitLogin2 ( ) method     public  String initlogin2 (Userbean userbean,model model)  {      // Declare a Userbean variable and call the Searchservice () method in the HelloService class to assign the resulting value to the userbean1       Userbean userbean1= helloservice.searchservice (UserBean);       // Set the Userbean change to Userbean1       model.addattribute ("Userbean", userbean1);        //Jump to login,jsp page        return   "Login";             }}

HelloService page comments are as follows:

Package Cn.training.service;import Org.springframework.stereotype.service;import cn.training.userbean.userbean;// @Service for labeling business layer Components @service//helloservice class public class HelloService {public UserBean searchservice (UserBean FIM) {//  Set the userid of FIM to test Fim.setuserid ("test"); return to FIM;}}

Userbean.java, helloworld.jsp, login.jsp, Web. XML, Springmvc-servlet.xml and other pages are omitted.

Precautions:

When creating a Controller , you need to implement it directly or indirectly Org.springframework.web.servlet.mvc.Controller interface.

@Autowired Annotation, it can annotate class member variables, methods, and constructors to complete the work of automatic assembly.

@RequestMapping requestmapping is an annotation to handle the request address mapping, which can be used on a class or method. On a class, the method that represents all response requests in a class is the parent path of the address. the requestmapping Annotation has six properties, which we'll describe in three categories.

1. value, method;

value :      " Span style= "FONT-SIZE:14PX; font-family: Arial "> Specifies the actual address of the request, the specified address can be uri Template mode;

method : Specify the requested method GET " post " put " delete  

2. consumes,produces;

consumes : Specifies the type of submission to process the request (content-type), such as Application/json, text/html;

produces: Specifies the type of content returned, only if Request in the request header (Accept) The type contains the specified type before returning;

3. params,headers;

params : Specifies that some parameter values must be included in the request before the method is processed.

Headers : Specifies that certain header values must be included in request for the method to process the request.

${userbean.userid} indicates that the userId in the Userbean is removed.

<form action= "Init" method= "POST" ><input name= "jsd" type= "text"/><button name= "Submit" type= "Submit" >login</button></form>

Through this code. I know there are many builds and controls in the XML file that can be used, such as text boxes and Submit buttons.

In such projects, involving more pages, should pay attention to the normative and consistent name, to avoid low-level errors.

About the spring MVC mechanism, sample interpretation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.