[JAVA] routing

routing

For each controller and method @requestmapping You can set the routing by giving. "Routing" in @RequestMapping of the controller, When "index" is specified in @RequestMapping of the method, The URL will be "http: // localhost: 8080 / routing / index". If you want to divide the method by HTTP method, you can specify it as "method = RequestMethod.GET".

Get query string in URL

To the method @requestparam integer id It can be specified in the same way as.

Get POST value

To the method @modelattribute indexform form It can be specified in the same way as. Please note that if there is no Setter in the class that holds the POST value, the value will not be set.

Sample program (routing, GET, POST value acquisition)

demo\src\main\java\com\example\demo\RoutingController.java


package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("routing")
public class RoutingController {

    // routing/Index processing(GET)
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public Model indexGet(Model model, @RequestParam(required=false) Integer id) {
        System.out.println("ID=" + id);

        return model;
    }

    // routing/Index processing(POST)
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Model indexPost(Model model, @ModelAttribute IndexForm form) {
        System.out.println("form.name=" + form.name);

        return model;
    }

    /**
     *Form class for holding POST values
     */
    public class IndexForm {
        public String name ;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
        
    }
}

Recommended Posts

routing
Nesting routing ...
About Rails routing
Rails Routing Basics
Rails 6.0 Routing Summary
[Rails] devise-related routing summary
[Note] Rails3 routing confirmation
Understand Rails "shallow" routing
[Rails] Complete routing settings