SPRINGMVC Note Description

Source: Internet
Author: User

@controller

You can define class as a controller class by @controller annotations.


@RequestMapping

Value Represents the format of the URL that needs to be matched.
Method Represents the HTTP protocol (such as Get,post,put,delete, etc.) that is required to process the request, and the optional value is the value of the enum Requestmethod.
Params The format is "Paramname=paramvalue" or "Paramname!=paramvalue". Indicates that the parameter must be equal to a value or not equal to enter this mapping method. Do not fill in the time to indicate that no limit
Headers The content that must be included in the headers that is used to qualify the corresponding Reqeust request, such as headers={"Connection=keep-alive"}, indicates that the value of Connection in the request header must be keep-alive.






@RequestParam

Value The value of the corresponding form name space
Required Whether to allow empty
DefaultValue Default value

@PathVariable

Get the parameters passed in the Address bar for example:

	@RequestMapping (value= "/{groupid}.do") public
	void Detail (@PathVariable long groupId) {
		Grouprepository.selectone (groupId);
	}

@RequestBody

Add the @requestbody annotation before the parameter. Converts the data format of the request parameter sent by the specified client to a Java entity

	@RequestMapping (value = "/xxxxx.do") public
	Void Create (@RequestBody () String host) {
		System.out.println ("--- --------"+ host";
	}

@RequestHeader

Add the @requestheader annotation before the parameter. The parameter that is used to allude the specified request header information to a method.

	@RequestMapping (value = "/xxxxx.do") public
	Void Create (@RequestHeader () multivaluemap<string, string> host {
		System.out.println ("-----------" + host);
	}

@ResponseBody

If this method defines the @responsebody annotation. The return value is then converted to this data format, which is output to the client

	@RequestMapping (value = "/xxx.do")
	@ResponseBody public
	multivaluemap<string, string> Create (@ Requestheader () multivaluemap<string, string> hosts) throws Exception {
		return hosts;
	}
@ResponseStatus
Returns a specified HTTP response status code.

    
    @ResponseStatus (reason= "No Reason", value=httpstatus.bad_request)
    @RequestMapping ("/responsestatus")
    public void Responsestatustest () {
      
    }


@SessionAttributes

Write the annotations at the class level, define a session attributes, and specify the property name Sessionattributes. You can specify multiple (arrays), and you can specify a type.

@Controller
@SessionAttributes ({"User"})
@RequestMapping ("/test") public
class Controllertest {
  @ Requestmapping ("/session")
  @ResponseBody public
  String Sessionin (@ModelAttribute ("user") of user user) {
  Return "index";
  } 
}

@CookieValue

@RequestMapping ("/cookie")
@ResponseBody public
string Cookie (@CookieValue ("Jsessionid") string SESSIONID) {
return sessionId;
}


@InitBinder

Register a customer Protperty editor in the controller to parse the parameters in the request and bind to the parameters in the handler method through the date bind mechanism.

 
    @InitBinder public
    void Initbinder (Webdatabinder binder) {
       SimpleDateFormat DateFormat = new SimpleDateFormat ( "Yyyy-mm-dd");
       Dateformat.setlenient (false);
       Binder.registercustomeditor (Date.class, New Customdateeditor (
              DateFormat, false));
    }

   @RequestMapping ("/databind1") public
    Modelandview databind1 (date date) {
      ...   
   }

Access URL http://localhost:8080/springmvc/databind1.action?date=2000-01-02
Automatically convert 2000-01-02 to a date type with the Customedateeditor type registered in Initbinder


@ExceptionHandler

  
   @RequestMapping ("/exception") public
    void Exceptiontest () throws exception{
       throw new Exception ("I don ' t know" );
    }  
    @ExceptionHandler public
    String handleexception (Exception e,httpservletrequest request) {
       System.out.println (E.getmessage ());
       return "HelloWorld";
    }


This article is part of a collection of other blog posts. If not all please add, if there are errors please correct me.






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.