Spring Learning Note 001

Source: Internet
Author: User

Reprinted from: Http://www.cnblogs.com/zplogo/p/5029918.htmlSpringMVC Learning Note 001 Scenario: SPRINGMVC sends a request to the background control layer, which processes the request (gets the data passed in, responds to the request , put some simple data into the request-as-model object) Add SPRINGMVC's Dispatcherservlet core controller to Web. XML to indicate which requests are blocked. The SPRINGMVC XML configuration file is established under the Web-inf folder, and Component-scan indicates the control class that is scanned under that package. Classes that are annotated by @controller are treated as control classes, @requestmapping (value= "", Method=requestmethod. Post) means that the request to process the class (the request that is matched by the value) is processed, and that the request can only be a POST request. About the @requestparam and model model objects, such as the method that processes the request string login (@RequestParam ("username") string Username,model Model) { Model.addattribute ("UserName", UserName)} represents the value assigned to userName from the request parameter UserName and then into the model object, which corresponds to the request, The objects stored inside can be obtained through the EL expression ${}.   @RequestParam take a value from the request parameter, if the action layer variable name and the request parameter name are the same, you do not need to @requestparam annotations to get the value. Q1: Scene: Directly enter the URL address to access the control layer to achieve the purpose of some functions, such as the list of "analog form submission" A1: can be sent by the browser request simulation form submission, such as http://localhost:8080/project/       Listviewer.spring?username=zhang If a request is intercepted with a ". Spring" end, the control-layer method will process listviewer the request and accept the value of the parameter username Q2: scenario: How does the control layer limit the request? A2: Add @requestmapping (value= "", Method=requestmethod.) on the request processing method. Post) indicates that a request sent by the foreground can only be a post Q3:Scenario: Troubleshoot multiple-person development paths that might be duplicated?       (such as the foreground login to send/login request, background login also send/login request) "To help the modular development" solution to the path duplication problem is to limit the access path of each module. A3: Two requests can be/a/login and/b/login, and the control layer has two classes that handle A/b request such as 1 @Controller2 @RequestMapping ("/A") 3 public class Login{4 @R   Equestmapping ("/login") 5 public String login () {} 6} If the @requestmapping annotation is used above the class, the relative parent path is defined first, and then the path defined on the method is relative to the class level. Q4: Path problem?     The controller response request is based on the current path, and the path to the response file is similar to the Linux switch directory.     The following configurations can be added to Springmvc-servlet.xml: Its function is to limit the default access to the/root path.    Note: In response to files in the same directory, the path does not add/, the general situation of all JSP files should be placed in the/web-inf directory, so as to prevent direct access through the path, must let the background processing after the jump.        Q5: The control layer redirects to the control layer-parameters and no parameter passing (the difference between the request and parameters) A5: such as the control layer processing method return "Redirect:/listusername.spring?username=zhang"; The key code for the SPRINGMVC redirect adds the "redirect:/" prefix to the return string.    Represents a listusername.spring request sent under the current directory. Q6: The matching URL path executes the specified controller (the control layer's handling method @requestmapping the value of the variable, indicating that the path to the request can be any value, which can be matched) A6: If the request is sent AS/HTTP        The localhost:8080/springmvc/login/a/login.spring represents a login request that is sent under the/login directory. Copy Code @controller@requestmapping ("/login/{subfolder}") public class login{@RequestMapping ("/login") public String Login (@PathVariable ("subfolder") String subfolder) {System.out.println (subfolder); return "/index.jsp";}} Copy the code printout A, where the parameters passed in the login method are the same as the names of the @requestmapping annotations above, and do not have to enter or exit values to @pathvariable.

Spring Learning Note 001

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.