Controller


The controller is the core of the MVC system. It is responsible for processing all requests initiated by the browser and determining the logical processing of response content. The controller is a standard Java class and does not need to inherit any base class. , expose the interface to the outside through the method in the class, and the return result of this method will determine the specific content of the response to the browser;

The following is an example of writing the controller in the WebMVC module:

@Controller
public class DemoController {

    @RequestMapping("/sayhi")
    public IView sayHi() {
        return View.textView("Hi, YMPer!");
    }
}

Start the Tomcat service and access http://localhost:8080/sayhi, the output result will be: Hi, YMPer!

From the above code, we can see that there are two annotations, namely:

  • @Controller: Declare a class as a controller, and the framework will automatically scan it when it starts. All classes that declare this annotation are registered as controllers;

    name: controller name, default is "" (this parameter is not used yet);

    singleton: specify the control Whether the controller is a singleton, the default is true;

  • @RequestMapping: declares the controller request path mapping, scope: class or method;

    value: Controller request path mapping, required parameter;

    method[]: Allowed request method, the default is GET method, value range: GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE;

    header[]: header name that must exist in the request header;

    param[]: parameter name that must exist in the request;

Example 1:

Create a non-singleton controller, the controller method rules are as follows:

  1. The controller method only supports POST and PUT access. ;
  2. The request header parameter must contain x-requested-with=XMLHttpRequest (that is, to determine whether it is an AJAX request);
  3. The name parameter must exist in the request parameter;
@Controller(singleton = false)
@RequestMapping("/demo")
public class DemoController {

    @RequestMapping(value = "/sayhi",
        method = {Type.HttpMethod.POST, Type.HttpMethod.PUT},
        header = {"x-requested-with=XMLHttpRequest"},
        param = {"name=*"})
    public IView sayHi() {
        return View.textView("Hi, YMPer!");
    }
}

Example description:

This example mainly shows how to use @Controller and @RequestMapping annotations to control controllers and controller methods Forward configuration;

The controller method must be modified with public, otherwise it will be invalid;

Since the @RequestMapping annotation is also declared on the controller, the request path mapping of the controller method will become: /demo/sayhi;

Example 2:

The above example shows some control over the request. The following shows how to control the response results. The rules are as follows:

  1. Set response header parameters through annotations:
    • from = "china"
    • age = 18
  2. Set through annotations The controller returns the view and content: "Hi, YMPer!"
@Controller
@RequestMapping("/demo")
public class DemoController {

    @RequestMapping("/sayhi")
    @ResponseView(value = "Hi, YMPer!", type = Type.View.TEXT)
    @ResponseHeader({
            @Header(name = "from", value = "china"),
            @Header(name = "age", value = "18", type = Type.HeaderType.INT)})
    public void sayHi() {
    }
}

Three annotations are used in this example:

  • @ResponseView: declares that the controller method returns the view object by default, and is only used when the method has no return value or the return value is invalid

    name: view template file path, default is "";

    type: view file type, default is Type.View.NULL;

  • @ResponseHeader: Add response header parameters when setting the controller method to return results;

    value[]: Response header @Header parameter collection;

  • @Header: Declare a request response Header key-value pair, only used for parameter transfer;

    name: Response header parameter name, required parameter;

    value: Response header parameter value, default is "";

    type: response header parameter type, supports STRING, INI, DATE, default is Type.HeaderType.STRING;