Five, Spring MVC practical Pojo for parameter passing value

Source: Internet
Author: User
Tags locale

As already mentioned, Spring MVC can be used by @pathvariable to map the values in the URL to the parameters of the target method, or you can use @requestparam to pass a single parameter, if you need to pass more than one parameter at a time, you need to write multiple corresponding annotations, Is there a simpler and more convenient way to do that? The answer is that spring MVC has the automatic boxing function, which allows the assignment to be done for the corresponding properties of the background bound object through the Name property of the parameter, and also supports cascading operations, which support nesting.


1. Use Pojo (Plain old Java Object) for parameter values

Here's a look at a specific example, defining a User object, an Address object:

public class User {private String userName;    private String password;    Private String Email;    private int age;    private address address; Omit the Get and set methods and the ToString Method ...}
public class Address {private String province;    Private String City; Omit get and set methods and ToString method}


The controller is defined as follows:

/** * Spring MVC can be automatically "boxed", according to the request parameter's name completion and the mapping binding between the Pojo property, * Automatically fills the property value for the object, and supports cascading properties * * @param user * @return */@RequestMappi    Ng ("/testpojo") public String Testpojo (user user) {System.out.println (user); return "greeting";}


The test code is as follows, with the properties of the user object, with a cascading property address:

<form action= "/testpojo/testpojo" method= "POST" > <p>pojo test, can directly pass parameters bound to Pojo objects </p> username:< Input type= "text" name= "UserName"/> <br/> password:<input type= "text" name= "password"/> <br/> E Mail:<input type= "text" name= "email"/> <br/> age:<input type= "text" name= "age"/> <br/> provi Nce:<input type= "text" name= "address.province"/> <br/> city:<input type= "text" name= "address.city"/ > <br/> <input type= "Submit" value= "Submit"/></form>


2. Parameter passing using the Servlet native API

Spring MVC supports the use of parameters such as ServletRequest and Servletresponse as the target method. Spring MVC supports the following types of ServletRequest, Servletresponse, HttpSession, Pricipal, Locale, InputStream, OutputStream, Reader, Writer, please refer to the source code in detail:

Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
@Overrideprotected  object resolvestandardargument (class<?> parametertype,  Nativewebrequest webrequest)  throws Exception {   HttpServletRequest  Request = webrequest.getnativerequest (Httpservletrequest.class);    Httpservletresponse response = webrequest.getnativeresponse (Httpservletresponse.class);    if  (ServletRequest.class.isAssignableFrom (parametertype)  | |          multipartrequest.class.isassignablefrom (ParameterType))  {      object nativerequest = webrequest.getnativerequest ( ParameterType);      if  (nativerequest == null)  {          throw new illegalstateexception (                 "Current request is not of type [" + parametertype.getname ()  + "]:   " + request");       }      return  nativeRequest;   }   else if  ( ServletResponse.class.isAssignableFrom (parametertype))  {       This.responseargumentused = true;      object nativeresponse  = webrequest.getnativeresponse (parametertype);      if  ( Nativeresponse == null)  {         throw new  illegalstateexception (                 "current response is not of type ["  +  Parametertype.getname ()  +  "]: "  + response);      }      return nativeResponse;   }    else if  (HttpSession.class.isAssignableFrom (parametertype))  {       return request.getsession ();   }   else if  ( Principal.class.isAssignableFrom (parametertype))  {      return  Request.getuserprincipal ();   }   else if  (Locale.class ==  ParameterType)  {      return requestcontextutils.getlocale (request);    }   else if  (InputStream.class.isAssignableFrom (ParameterType))  {      return request.getinputstream ();   }    else if  (Reader.class.isAssignableFrom (parametertype))  {       return request.getreader ();    }   else if  (OutputStream.class.isAssignableFrom (parametertype))  {       this.responseArgumentUsed = true;       Return response.getoutputstream ();   }   else if  ( Writer.class.isAssignableFrom (parametertype))  {       This.responseargumentused = true;      return response.getwriter ( );    }   return super.resolvestandardargument (parameterType,  WebRequest);}


Project Source code:

Https://git.oschina.net/acesdream/spring-mvc



This article is from "Ace's Dream" blog, please be sure to keep this source http://acesdream.blog.51cto.com/10029622/1905903

Five, Spring MVC practical Pojo for parameter passing value

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.