Controller and Requestmapping

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>Defining controllers with @controller</strong><br>Spring MVC distributes requests for specific URLs to the controller class for processing<br>In spring 3.0, class is defined as a controller class by @controller Annotations. In order for spring to find a bean defined as a controller, you need to add the following definition in the Spring-context configuration File.</p></p><pre><pre><context:component-scan base-<span style="color: #0000ff;"></span> package</pre></pre><p><p><strong>Use @requestmapping annotations to correlate URLs and controllers</strong><br>With @requestmapping annotations, You can bind a specific URL to a method in a specific controller class or controller class. Such as</p></p><pre><pre><span style="color: #000000;">@Controller @requestmapping (</span>"/helloworld"<span style="color: #000000;">)</span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #000000;">Helloworld { @RequestMapping (method</span>=<span style="color: #000000;">requestmethod.get) </span>public<span style="color: #0000ff;"></span> <span style="color: #000000;"> Modelandview Hello () { </span><span style="color: #0000ff;">new</span> <span style="color: #000000;"> Modelandview (); Mv.setviewname (</span>"helloworld"<span style="color: #000000;">); </span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">mv; }}</span></pre></pre><p><p>Bind the controller Class/hellowrold and hellowrold, and the @requestmapping (method=requestmethod.get) before the Hello () method will be the Hello method and/ The GET request of the hellorworld.action is bound together.</p></p><p><p><strong>Define Multiaction Controllers</strong><br>Use a controller to handle multiple actions, called Multiaction Controllers. @RequestMapping annotations are not required to be placed before the class definition, but can be used directly as a label for a method level, and when used, the Contorller class becomes a multiaction controller, such as</p></p><pre><span style="color: #000000;"><span style="color: #000000;">@Controller</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Multiactionhelloworld {@RequestMapping (</span></span>"/hello1"<span style="color: #000000;"><span style="color: #000000;">) </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">modelandview hello1 () {modelandview mv</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Modelandview (); Mv.setviewname (</span></span>"helloworld"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">mv; } @RequestMapping (</span></span>"/hello2"<span style="color: #000000;"><span style="color: #000000;">) </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">modelandview Hello2 () {modelandview mv</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Modelandview (); Mv.setviewname (</span></span>"helloworld"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">mv; }</span></span></pre><p><p>The two methods Hello1 and Hello2 are defined here respectively, and the corresponding URLs are/hello1 And/hello2</p></p><p><p></p></p><p><p><strong>URL Template</strong><br>The URL in the @requestmapping parameter, in addition to the regular url, can use the URL template to define a URL that forms a similar rest request.<br>For example</p></p><pre><pre> @RequestMapping ("/urltemplate/{username}" <span style="color: #000000;">) </span> <span style="color: #0000ff;">public </span> <span style="color: #000000;"> Modelandview test1 (@PathVariable String username) {modelandview mv = <span style="color: #0000ff;">new </span> <span style="color: #000000;"> Modelandview (); Mv.addobject (</span> "userName" <span style="color: #000000;">, userName); System.out.println (username); Mv.setviewname (</span> "test002" <span style="color: #000000;">); </span> <span style="color: #0000ff;">return </span> <span style="color: #000000;"> mv; } </span> <span style= "white-space:normal;" ></span> </span></pre></pre><p><p>The above is bound with the values of {username} and {userid} in the request url, respectively, by @pathvariable the username and userid in the incoming parameter<br><br><strong>Optional parameters for @requestmapping</strong><br><strong>value</strong>: Represents the format of the URL that needs to be matched.<br><strong>method</strong>: Represents the HTTP protocol required to process the request (such as get,post,put,delete, etc.), and the optional value is the value of the enum REQUESTMETHOD.<br><strong>params</strong>: The format is "paramname=paramvalue" or "paramname!=paramvalue". Without parameters, the paramvalue can be any Value.<br>such as params = {"param1=1", "param2!=2", "param3"}, The corresponding URL must include PARAM1,PARAM2,PARAM3 three parameters, where the value of param1 must be 1,PARAM2 value cannot be 2, The value of the PARAM3 can be any Value.<br><strong>headers</strong>: The content that must be included in the headers that is used to qualify the corresponding reqeust request, such as<br>headers={"connection=keep-alive"}, which indicates that the value of Connection in the request header must be Keep-alive.</p></p><p><p></p></p><p><p>Controller and Requestmapping</p></p></span>

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.