Spring MVC Controller and binding knowledge point _spring

Source: Internet
Author: User

First, Controller

1, with @controller label as this is a controller

2, requestmapping (URL) used to mark the request path, divided into class-level and method-level requests.

3, the general controller method of writing, can have three kinds of forms.

The basic way:

@RequestMapping (value= "/view")

Public String GetCourse (@RequestParam ("CourseID") Integer CourseID)

Where @requestparam is the binding parameter. In general, the foreground pass parameter and the parameter name in the method are consistent, do not specify the @requestparam method binding.

Modern Mode "

@RequestMapping (value= "/view2/{courseid}")-----request path contains parameters

Public String GetCourse (@PathVariable ("CourseID") Integer CourseID)

Traditional mode: You may want to use the servlet API

@RequestMapping (value= "/VIEW3")

The public String getcourse (Integer courseid,httpservletrequest reques)----The servlet API can be placed directly in the parameter.

Ii. the difference between STRUTS2 and SPRINGMVC through controller

1, Struts2 is not annotated

2, the parameter binding, struts2 commonly uses Inherits modeldriver< Class > interface, and defines the user attribute in the class. The receive parameter is not defined in the method. The SPRINGMVC parameters are separate, each put on a separate definition parameter, and the class does not use member properties.

3, the use of the Servlet API. There are three ways to struts2, either through Actioncontext, the latter implementing *aware interfaces, or by using Servletactioncontext static methods. And SPRINGMVC can be used directly on the parameters.

4. Look at the return type, the return type of Struts2 is string, and the string is used to find the results page in the configuration file. And the SPRINGMVC return result is the Modelandview type. Contains the return parameters and URLs without adding parameters in the form of the Sevlet API.

Three, Bing parameters

is to populate the Model object with the fields in the request by name matching.

SPRINGMVC request redirection, you can write: return "redirect:view2/" +course.getcourseid ();

Return to the multi-level directory structure, you can directly write the relative path back "Course_admin/edit";

Method can directly use Type objects, Course Course---Spring can be boxed automatically


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.