13SPRINGMVC_ defines a business control method that only allows access to a GET or POST request

Source: Internet
Author: User

---restore content starts---

The function to be implemented in this article is that in an action, some business methods can only be executed by a post submission, and some methods can only be executed if the get is submitted.

such as the Useraction.java in the previous article (code below)

/*** Create by Shen Xiaoquan * Create on August 8, 2016 PM 8:53:34*/ PackageCom.guigu.shen.Action6;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;/*** * Request path can be split into: Root Module name + sub-module name is equivalent to when access to http://127.0.0.1:8080: project name/user/register will enter the Registermethod method. */@Controller @requestmapping (value= "/user")//request name of the root module Public classuseraction {/** Employee Registration **/@RequestMapping (Value= "/register")//request name of the sub-module/** Focus on the parameter names I write here. String username,string Salary The name of the Username,salary and * form submitted here is identical. The magic is that as long as the guarantee is exactly the same, you can collect the values on the page, * This is also SPRINGMVC and Struts2 very different point, spring is based on parameters to collect data. Struts2 is based on the entity to collect data. The cause of this problem is SPRINGMVC inside the action (Controller class is a singleton mode), if we adopt and STRUTS2 * In a way, inside define an EMP emp so, EMP only one, such as by 80 people submit this request to execute this method, * Will cause the 80th person to replace the previous person's data. So springmvc do so, 80 people do 80 methods, you can save every time * data. *  *  */ PublicString Registermethod (Model model,string username,string salary) {Model.addattribute ("Message", "Employee registration successful"); /** The username parameter is assigned as long as it is the same as the Name property on the page. * Salary*/System.out.print ("Information of the employees" +username+ ";" +salary); return"/jsp/success.jsp";} @RequestMapping (Value= "/login")//request name of the sub-module PublicString Loginmethod (Model model,string username) {Model.addattribute ("Message", "Employee registration successful");    SYSTEM.OUT.PRINTLN (username); return"/jsp/success.jsp"; }}

I want to change the public String registermethod (Model model,string username,string salary) above to a POST request only.

The above public String loginmethod (Model model,string username) can accept a POST request as well as a GET request.

How to solve it? Through annotations.

The code is modified as follows:

 PackageCom.guigu.shen.Action6;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;/*** * Request path can be split into: Root Module name + sub-module name is equivalent to when access to http://127.0.0.1:8080: project name/user/register will enter the Registermethod method. */@Controller @requestmapping (value= "/user")//request name of the root module Public classuseraction {/** Employee Registration **/@RequestMapping (Method=requestmethod.post,value= "/register")//request name of the sub-module/** Focus on the parameter names I write here. String username,string Salary The name of the Username,salary and * form submitted here is identical. The magic is that as long as the guarantee is exactly the same, you can collect the values on the page, * This is also SPRINGMVC and Struts2 very different point, spring is based on parameters to collect data. Struts2 is based on the entity to collect data. The cause of this problem is SPRINGMVC inside the action (Controller class is a singleton mode), if we adopt and STRUTS2 * In a way, inside define an EMP emp so, EMP only one, such as by 80 people submit this request to execute this method, * Will cause the 80th person to replace the previous person's data. So springmvc do so, 80 people do 80 methods, you can save every time * data. *  *  */ PublicString Registermethod (Model model,string username,string salary) {Model.addattribute ("Message", "Employee registration successful"); /** The username parameter is assigned as long as it is the same as the Name property on the page. * Salary*/System.out.print ("Information of the employees" +username+ ";" +salary); return"/jsp/success.jsp";} @RequestMapping (Value= "/login", Method={requestmethod.post,requestmethod.get})//request name of the sub-module PublicString Loginmethod (Model model,string username) {Model.addattribute ("Message", "Employee registration successful");    SYSTEM.OUT.PRINTLN (username); return"/jsp/success.jsp"; }}

Note: If you do not write Method=requestmethod. Post , both get and post requests support

13SPRINGMVC_ defines a business control method that only allows access to a GET or POST request

Related Article

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.