SPRINGMVC Note Notes

Source: Internet
Author: User
Tags bind dateformat

@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.) on which the request is to be processed, with the optional value requestmethod the value of this enum.
Params The format is "Paramname=paramvalue" or "Paramname!=paramvalue". Indicates that the parameter must equal a value or is not equal to entering this mapping method. Do not fill out the time indicated that there is no limit
Headers The content that must be included in the headers to qualify the corresponding Reqeust request, such as headers={"Connection=keep-alive"}, indicates that the Connection value in the request header must be keep-alive.






@RequestParam

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

@PathVariable

Get the parameters that are 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 insinuate the specified request header information as a method.

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

@ResponseBody

If this method defines a @responsebody annotation. Then the return value will be converted to this data format, 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 annotation at class level, define a session attributes, the attribute name is sessionattributes specified. You can specify more than one (array), and you can specify a type.

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


@CookieValue

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


@InitBinder

Register a customer Protperty Editor in controller to parse the parameters in the request and bind the parameters in handler method with 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 converts 2000-01-02 to date type through the type of customedateeditor 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 blogs. If there are not all please add, if there is a mistake 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.