Spring MVC HTTP Request address mapping

Source: Internet
Author: User

 PackageCom.springmvc.controller;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.servlet.ModelAndView;/*** <p> Pre-Preparation: Understand the structure and content of HTTP request messages. </p> * <p> Test HTTP request address mapping. </p> * Author:liaody*/@Controller Public classViewcontroller {/*** <p> Simple test MVC framework is built to complete </p> * *@returnView name*/@RequestMapping ("/view")     PublicString View () {System.out.println ("You have passed SPRINGMVC into the controller method .... "); return"Index"; }    /*** <p> Limited by URL: *. </p> * <p> Example: * value= "/user/*\/createuser, matching User/createuser, User/aaa/createuser, User/bb/createuser.     * represents 0 or more characters. * </p> * <p> * Access URL:http://localhost: 8080/user/createuser, url:http://localhost: 8080/user/aaa/createuser, url:http://localhost: 8080/user/bb/createuser * </p> * *@return     */@RequestMapping (Value= "/user/*/createuser")     PublicModelandview Paramurl () {Modelandview mv=NewModelandview (); System.out.println ("Call Paramurl Method"); Mv.addobject ("Message", "User/*/createuser match: Url:http://localhost:8080/user/createuser"); Mv.addobject ("Message2", "User/*/createuser match: Url:http://localhost:8080/user/aaa/createuser"); Mv.addobject ("Message3", "User/*/createuser match: Url:http://localhost:8080/user/bb/createuser"); Mv.setviewname ("/success"); returnMV; }    /*** <p> Limited by URL: * *. </p> * <p> Example: * value= "/user/**\/createuser, matching User/createuser, User/aa/bb/createuser, User/aa/bb/cc/crea Teuser.     * * Delegates can have multiple paths. * </p> * <p> * Access URL:http://localhost: 8080/user/createuser,http://localhost: 8080/user/aa/bb/createuser,http://localhost: 8080/user/aa/bb/cc/createuser * </p> * *@return     */@RequestMapping (Value= "User/**/createuser")     PublicModelandview ParamUrl2 () {Modelandview mv=NewModelandview (); System.out.println ("Call ParamUrl2 Method"); Mv.addobject ("Message", "User/**/createuser match: Url:http://localhost:8080/user/createuser"); Mv.addobject ("Message2", "User/**/createuser match: Url:http://localhost:8080/user/aa/bb/createuser"); Mv.addobject ("Message3", "User/**/createuser match: Url:http://localhost:8080/user/aa/bb/cc/createuser"); Mv.setviewname ("/success"); returnMV; }    /*** <p> URL-qualified: {} placeholder. </p> * <p> Example: * value= "/user/**\/{userid}, matching/user/aa/123,/USER/AA/BB/ABC,/USER/AA/BB/CC/DDD * < /p> * <p> * access to Url:url:http://localhost: 8080/user/aa/123,http://localhost: 8080/USER/AA/BB/ABC,http://localhost: 8080/USER/AA/BB/CC/DDD * </p> * *@return     */@RequestMapping (Value= "User/**/{userid}")     PublicModelandview ParamUrl3 (@PathVariable ("UserId") String userId) {modelandview mv=NewModelandview (); System.out.println ("Call ParamUrl3 Method"); System.out.println ("Binding parameters with @pathvariable annotations userid:" +userId); Mv.addobject ("Message", "User/**/{userid} match: Url:http://localhost:8080/user/aa/123"); Mv.addobject ("Message2", "User/**/{userid} matches: Url:http://localhost:8080/user/aa/bb/abc"); Mv.addobject ("Message3", "User/**/{userid} matches: Url:http://localhost:8080/user/aa/bb/cc/ddd"); Mv.setviewname ("/success"); returnMV; }    /*** <p> via URL limit:?.     </p> * <p> Example: * value= '/user/createuser??, matching User/createuseraa, User/createuserbb.? represents 0 or one character. * </p> * <p> * Access URL:http://localhost: 8080/user/createuseraa,http://localhost: 8080/USER/CREATEUSERBB * </p> * *@return     */@RequestMapping (Value= "/user/createuser??")     PublicModelandview ParamUrl4 () {Modelandview mv=NewModelandview (); System.out.println ("Call ParamUrl4 Method"); Mv.addobject ("Message", "User/createuser??" Matching: Url:http://localhost:8080/user/createuseraa "); Mv.addobject ("Message2", "User/createuser??" Matching: URL:HTTP://LOCALHOST:8080/USER/CREATEUSERBB "); Mv.setviewname ("/success"); returnMV; }    /*** <p> by URL: {XXX} binding xxx parameter value.     </p> * <p> Example: * value= "/owners/{ownerid}, matching owners/123, OWNERS/ABC * </p> * <p> * URL to access:http://localhost: 8080/owners/123,http://localhost: 8080/OWNERS/ABC * </p> * <p> * If @pathvariable does not specify a parameter name, the debug switch is only available when the window is compiled and is not recommended. * <p> * </p> * *@return     */@RequestMapping (Value= "/owners/{ownerid}")     PublicModelandview ParamUrl5 (@PathVariable ("ownerID") String ownerid) {modelandview mv=NewModelandview (); System.out.println ("Call ParamUrl5 Method"); Mv.addobject ("Message", "/owners/{ownerid} match: Url:http://localhost:8080/owners/123"); Mv.addobject ("Message2", "User/createuser??" Matching: URL:HTTP://LOCALHOST:8080/OWNERS/ABC "); Mv.addobject ("Message3", "Parameters bound" +ownerid); Mv.setviewname ("/success"); returnMV; }    /*** <p> by request, generally only use Get and post, PUT, delete not used </p> * <p> by request method to limit:post</p> * <p > * Access: Use post to access Http://localhost:8080//method/post * </p> * *@return     */@RequestMapping (Value= "/method/post", method =requestmethod.post) PublicModelandview Methodpost () {Modelandview mv=NewModelandview (); System.out.println ("The Post method is called"); Mv.setviewname ("/success"); Mv.addobject ("Message", "the Post method was successfully called"); returnMV; }    /*** <p> by request method Limit:get</p> * <p>value value intentionally set the same as above value= "/method/post" </p> * <p> * Access: Access http://localhost:8080//method/post using GET mode * *@return     */@RequestMapping (Value= "/method/post", method =requestmethod.get) PublicModelandview Methodget () {Modelandview mv=NewModelandview (); System.out.println ("Call is a Get method"); Mv.setviewname ("/success"); Mv.addobject ("Message", "successful call after Get Method!" "); returnMV; }    /*** <p> by request parameter Limit </p> * <p> * "param1": indicates that the request must contain a request parameter named param1.     * "!PARAM1": indicates that the request cannot contain a request parameter named param1.     * "!param1=value1": Indicates that the request contains a request parameter named PARAM1 and its value cannot be value1.     * {"Param1!=value11", "param2"}: Indicates that the request contains a request parameter named param1 and Param2 and that the param1 value cannot be value1. * </p> * <p> URL to access:http://localhost:8080/param1/yuyu?username=liaody&userpassword=123</p> * *@paramValidationType *@paramUserName *@paramUserPassword *@return     */@RequestMapping (Value= "/param1/{validation}", params = {"Username!=administrator", "UserPassword"})     PublicModelandview param1 (@PathVariable ("Validation") String ValidationType, @RequestParam ("UserName") String UserName, @RequestParam ("UserPassword") (String userpassword) {System.out.println ("Binding parameter Setting {}:" +ValidationType); System.out.println ("Binding parameter Username:" +userName); System.out.println ("Binding parameter Setting UserPassword:" +UserPassword); Modelandview MV=NewModelandview (); Mv.setviewname ("/success"); returnMV; }    /*** <p> by the request header parameter limit </p> * <p> access URL:http://localhost:8080/param1/yuyu?username=liaody&userpassword=123</p> * *@paramUserId *@return     */@RequestMapping (Value= "/head", headers= "content-type=text/*")     PublicModelandview Head (@RequestParam ("UserId")) (String userId) {System.out.println ("Binding parameter Setting {}:" +userId); Modelandview MV=NewModelandview (); Mv.setviewname ("/success"); Mv.addobject ("Message", "qualified by the request header parameter!" "); Mv.addobject ("Message2", "Binding parameter userid! "+userId); Mv.setviewname ("/success"); returnMV; }}

success.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Demo Login Success Page <br >${message}<br>${message2}<br>${message3}</body>

Spring MVC HTTP Request address mapping

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.