SpringMVC study summarized

SpringMVC


-----------------------------
POJO: Plain Ordinary Java Object simple Java objects, is in fact an ordinary JavaBeans, EJB and to avoid referred to the confusion created by
viewresolver: 


    <bean class="">
        <property name ="prefix" value="前缀"></>
        <property name ="sufix" value=".jsp"></>
    </>


@RequestMapping : can be used to process the request
    @RequestMapping ( "/ hello") // hello request is the name of
    the added class for each method of the class are combined with a directory
    list of parameters:
        value : Default
        Method : mode request , for example: RequestMethod.POST
        params : specified request parameters, I want to!
            params1: indicates a request must contain a request parameter named param1
                eg: params = { "username" }: sending a request when a named parameter must bring the usernam, otherwise 404
            params1:! not take this parameter indicates
                eg: params = { "! username"}: sending a request can not be put in a parameter named usernam, otherwise 404
            param1 = VALUE1:! indicates a request containing request parameter named param1, and its value can not be VALUE1
                EG: the params = { "username ! = 123 "}: when the transmission request; username value must not be carried by 123 (with or without username will do)


        headers : request headers, you can specify which browser which can not be accessed
        Consumes : only accept content type is the kind of request, specified request header-Type Content
        Produces : tell what type of browser return is added to the response headers Type-on Content: text / HTML; charset = UTF-8
Ant-style match :
    ? : Matches one character
    *: matches a character or a directory
    **: multilayer matching directory
@PathVariable :
    placeholders on the path: @RequestMapping ( "/ user / # {id}") Example: / item / user / aaa
    for placeholder in the method:
        public void pathVariable (@PathVariable ( "ID") String ID) {}


REST recommendation of URL
    url so a name: / resource Fu / resource represents operators
    / book / 1 GET query No. 1 book
    / book / 2 PUT Update No. 2 books
    / book / 1 DELETE to delete a good book
    / book POST increase a book
   URL address architecture design - so simple URL to submit a request to the request to operate the way resources

Can only be initiated from the page GET, POST request, if you want to send PUT, DELETE requests, Spring provides support for REST-
    1), SpringMVC has a Filter, he can request it into an ordinary request specified
        in web.xml configure the Filter


        <filter>
         <filter-name>HiddenHttpMethodFilter</filter-name>
         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
        <filter-mapping>
         <filter-name>HiddenHttpMethodFilter</filter-name>
         <url-pattern>/*</url-pattern>
        </filter-mapping>


    2), how to initiate other requests ?
        1, to create a POST form
        2, the table item contains the parameters of a _method,
        3, can return the _method the specified type of request


             <form action="Book/1" method="post">
                <input name="_method" value="put"/>
                    <input type="submit" value="更新一个图书">
             </form>


           * Note: Due to the high version of tomcat jsp support REST constraints problems may occur 405, the solution is to add a isErrorpage = "true" in the jsp page to receive this exception, not to cause an exception thrown 405


How to obtain the parameters of the request SpringMVC brought
    @RequestParam ; acquisition request parameters, there is a default, the parameter name parameter with the direct url parameter list is the same as on the Ok
    Example: / Book username = 123?
        @ RequestMapping ( "/ Book")
        public void get (String username) may get to 123

        / Book? the User = 123
        @ RequestMapping ( "/ Book")
        public void GET (String username) can not get to the value, null

        ? / Book username = 123
        @ RequestMapping ( "/ Book")
        public void GET (@RequestParam ( "username") String name) can get to 123
    @RequestHeader: Gets the value of a key request header of
    @CookieValue: also uninstall parameters list to get Cookie
------------------------------------------- -------
SPRINGMVC automatic packaging **
    Example: @RequestMapping ( "/ book")
         public String the addBook (Book book) {}
         may be the data request to the automatic packaging book which
    
    may be written directly to the native the API
    public String Handler (HttpServletSession, the HttpServletRequest)
-------------------------------------------- ------------------
a filter provided CharacterEncodingFilter Spring

  1. <-! Request solve the garbage problem ->
  2.   <filter>
  3.     <filter-name>CharaoterEncodingFilter</filter-name>
  4.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  5.     <! - solve garbled GET POST request is addressed garbled on the server ->
  6.     <init-param>
  7.       <param-name>encoding</param-name>
  8.       <param-value>UTF-8</param-value>
  9.     </init-param>
  10.     <! - solve garbled response ->
  11.     <init-param>
  12.       <param-name>forceEncoding</param-name>
  13.       <param-value>true</param-value>
  14.     </init-param>
  15.   </filter>
  16.   <filter-mapping>
  17.     <filter-name>CharaoterEncodingFilter</filter-name>
  18.     <url-pattern>/*</url-pattern>
  19.   </filter-mapping>

  Note that filter configuration order presented to resolve character encoding must be equipped at the top
 

 To be continued ...

Published 32 original articles · won praise 13 · views 6880

Guess you like

Origin blog.csdn.net/weixin_43938351/article/details/104579368