Annotation-based controller in spring mvc

Source: Internet
Author: User

Annotation-based controller in spring mvc

The annotation-based controller can contain multiple request processing methods in a controller class. This article describes annotation classes that are frequently used in spring mvc, including:

The annotation that declares Controller, Action, and URL ing (@ Controller, @ RequestMapping) transmits data (Model) to the view to accept the annotation of Request Parameters and PATH variables (@ RequestParam, @ PathVariable) redirectAttributes returns the JSON data annotation. @ ModelAttribute is used to accept the request object declaration Controller, Action, and URL ing annotation.

In the previous blog, we used the following code to declare the Controller and Action, and to declare the URL address of their response.

@Controller@RequestMapping(value = "/home")public class HomeController {    @RequestMapping(value = "/index")    public String Index(Model model) {        model.addAttribute("msg", "hello,springmvc");        return "index";    }}

@ Controller indicates that the current class is a Controller class, And the @ RequestMapping annotation is used on the method, indicating that the method is an Action method and responds to the URL request declared by value. If @ RequestMapping is also used in the Controller, the Index method responds to the "/home/index" request in the code above.

Passing data (Model) to a view)

The above codemodel.addAttribute("msg", "hello,springmvc");Assign a string to an org. springframework. ui. model object. The spring mvc Framework will pass this object to the View File. In the View File, you can use the EL expression to obtain and enter org. springframework. ui. data in the Model object.

${msg}
Accept the annotation of Request Parameters and PATH variables

The following two methods receive the request parameters and PATH variables respectively.

@RequestMapping("/person")public String Person(@RequestParam int id) {    System.out.println("id=" + id);    return "person";}@RequestMapping("/person/{name}")public String Person2(@PathVariable String name) {    System.out.println("name=" + name);    return "person";}

The Controller class ing/home of the two methods. When the request is/home/person? When id = 123, we use @ RequestParam annotation for the id object, and the spring mvc Framework will convert the id value in the request parameter to the type we need, then assign the value to the id parameter of the action method. When the request is/home/person/kai, {name} is used as the path parameter. The corresponding part of the url ("kai") is assigned to the name parameter of the action method.

Data parameters passed during redirection

Use org. springframework. ui. the Model cannot pass data to the redirected View File. You need to use org. springframework. web. servlet. mvc. support. redirectAttributes object.

@RequestMapping("/do")public String Do(RedirectAttributes redirectAttributes){    redirectAttributes.addFlashAttribute("msg", "redirect data");    return "redirect:/home/person/kai";}

Here we use redirectAttributes. addFlashAttribute () method to put data in org. springframework. web. servlet. mvc. support. in the RedirectAttributes object, data can be obtained through EL expressions in the redirected view.

Returns the annotation of JSON data.

If you want the action method to return json data, you need to do the following:
1. Enable the spring MVC configuration file As follows:


  
      
       
        
         
       
        
         
          
         
        
       
      
     
    
   
  

Second, introduce the dependencies of the jackson package as follows:


      
   
    org.codehaus.jackson
       jackson-mapper-asl    
   
    1.9.2
   
  

Third, use the @ ResponseBody annotation.
First, we define a model class.

package springmvc2.models;public class Person {    private String id;    private String name;    private int age;    //getters and setters}

The following is the action method.

@RequestMapping(value="/getinfo",method = RequestMethod.GET)public @ResponseBody Person GetInfo(){    Person p=new Person();    p.setId("abc");    p.setName("kai");    p.setAge(18);    return p;}

When you request/home/getinfo, the following content is returned:

{"id":"abc","name":"kai","age":18}
Use @ ModelAttribute to accept the request object

As mentioned above, @ RequestParam and @ PathVariable, each annotation accepts a parameter. If a registration page contains multiple form elements, you can use @ ModelAttribute to accept the entire object after submission. The following is a form in the JSP file.

 

Each input element is a property of the Person class. I can obtain the Person object in the following way:

@RequestMapping(value="/addperson",method=RequestMethod.POST)public String AddPerson(@ModelAttribute Person person) {    System.out.println("person.id=" + person.getId());    System.out.println("person.name=" + person.getName());    System.out.println("person.age=" + person.getAge());    return "person";}

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.