annotation configuration springmvc and assigning values to scopes

Source: Internet
Author: User

1. Configuring the package scanner in Applicationcontext.xml

<!---    <base-package = " Cn.cnsdhzzl.controller "/>

2. Configuring the Controller

/*Processor*/@Controller/*name Space*/@RequestMapping ("/annotation") Public classAnnotationcontroller {/*URL of the mapping request*/@RequestMapping (Value= "/fist.do")     PublicString One () {System.out.println (The first method of annotations); /*Resource View logical name*/        return"Login"; }    /*use annotations to pass parameters, and set the parameter is not necessary, when using the annotation tag to pass the parameter, do not pass the error, do not use the annotation label default Required=false, do not pass error*/@RequestMapping (Value= "/two.do")     PublicString, @RequestParam (required =false) (String text) {System.out.println (The second method of annotations);        System.out.println (text); return"Login"; }    /*Automatic assembly Parameters*/@RequestMapping (Value= "/three.do")     Publicstring Three (string text) {System.out.println (The third method of annotations);        System.out.println (text); return"Login"; }    /*use model to pass value to page, model is a map (key value pair)*/@RequestMapping (Value= "/four.do")     Publicstring Four (string text, model model) {Model.addattribute ("Model", "HelloAnnotation4"); System.out.println ("Fourth method of annotations");        System.out.println (text); return"Login"; }    /*do not write key by default to get values by type*/@RequestMapping (Value= "/five.do")     Publicstring Five (string text, model model) {Model.addattribute ("HelloAnnotation5"); System.out.println ("Fifth method of annotations");        System.out.println (text); return"Login"; }    /*use rest style to carry parameters, you need to configure web.xml:<url-pattern>*.do</url-pattern>*/@RequestMapping (Value= "/six/{text}")     PublicString Six (@RequestParam (required=false) @PathVariable String text, model model) {Model.addattribute ("Model", text); System.out.println ("Sixth method of annotations");        System.out.println (text); return"Login"; }    /*return directly to Modelandview*/@RequestMapping (Value= "/seven.do")     PublicModelandview Seven () {Modelandview mv=NewModelandview (); Mv.setviewname ("Login"); Mv.addobject ("Model", "Modelandview"); returnMV; }}

annotation configuration springmvc and assigning values to scopes

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.