SPRINGMVC parameters obtained by annotations

Source: Internet
Author: User

SPRINGMVC can be mapped by Requestparam annotations to obtain parameters, using the following:

Example:

Configuration procedure omitted

1. New Controller class

  

1  PackageCom.loger.controller;2 3 ImportOrg.springframework.stereotype.Controller;4 Importorg.springframework.web.bind.annotation.RequestMapping;5 6 7 @Controller8  Public classRequestparam {9     Ten      Public Static FinalString SUCCESS = "SUCCESS"; One      A@RequestMapping (value= "/requestparam") -      PublicString Requestparam (@org. springframework.web.bind.annotation. -Requestparam (value= "username") String un, the@org. Springframework.web.bind.annotation.RequestParam (value= "Age") (Integer age) { -          -System.out.println (un + "" +Age ); -          +         returnSUCCESS; -     } +}

2.index.jsp

Operation Result:

Add: If the table sole name is consistent with the parameter name of the method, you do not need to use the @requestparam annotation to map it.

If you change to

@RequestMapping (value= "/requestparam") public     string Requestparam (String Username,integer-age)!


Use the class as an argument and include the parameter-fetching method for the cascading property
:
1. New Adress class
  
1  PackageCom.loger.bean;2 3  Public classAddress {4     PrivateString Province;5     PrivateString City;6      PublicString getprovince () {7         returnProvince;8     }9      Public voidsetprovince (String province) {Ten          This. Province =Province; One     } A      PublicString getcity () { -         returnCity ; -     } the      Public voidsetcity (String city) { -          This. City =City ; -     } - @Override +      PublicString toString () { -         return"Address [province=" + Province + ", city=" + City + "]"; +     } A      at}

2. New User class

1  PackageCom.loger.bean;2 3  Public classAddress {4     PrivateString Province;5     PrivateString City;6      PublicString getprovince () {7         returnProvince;8     }9      Public voidsetprovince (String province) {Ten          This. Province =Province; One     } A      PublicString getcity () { -         returnCity ; -     } the      Public voidsetcity (String city) { -          This. City =City ; -     } - @Override +      PublicString toString () { -         return"Address [province=" + Province + ", city=" + City + "]"; +     } A      at}

3.controller

4. Forms

User has a cascading attribute address, and the form passed in with the parameter address.city address.province

1 <formAction= "Pojoparam">2Name:<inputtype= "text"name= "Name"><BR>3Age:<inputtype= "text"name= "Age"><BR>4City:<inputtype= "text"name= "Address.city"><BR>5Provinces:<inputtype= "text"name= "Address.province"><BR>6         <inputtype= "Submit"value= "Submit"><BR>7     </form>

Operation Result:






SPRINGMVC parameters obtained by annotations

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.