Spring MVC several ways to get path parameters-light summer's personal space-open source China community

Source: Internet
Author: User

<title>Spring MVC several ways to get path parameters-light summer's personal space-open source China community</title>

SPRINGMVC is a dispatcherservlet-based MVC framework that each request is first visited by Dispatcherservlet, Dispatcherservlet is responsible for forwarding each request to the corresponding Handler,handler processing and then returning the corresponding view and model, the returned view and model can not be specified, That is, you can return only the model or only the view or none of the returns. In SPRINGMVC using annotations, processor handler is based on @controller and @requestmapping annotations, @Controller declares a processor class, @RequestMapping declares the mapping of the corresponding request , which provides a very flexible way to match and handle.

?

Dispatcherservlet is inherited from the HttpServlet, since SPRINGMVC is based on Dispatcherservlet, then we first configure the Dispatcherservlet, So that it can manage what we want it to manage. The HttpServlet is declared in the Web. xml file.


Pass a value from the view to the controller? Controller <---View

1. Get the pass parameter in the path through @pathvariabl annotations?

?
1234567 1?????@RequestMapping(value?=?"/{id}/{str}")?2?????public?ModelAndView?helloWorld(@PathVariable?String?id,?3?????????????@PathVariable?String?str)?{?4?????????System.out.println(id);?5?????????System.out.println(str);?6?????????return?new?ModelAndView("/helloWorld");?7?????}

2.
?
1) Simple type, such as int, String, should be @requestparam annotated before the variable name,
For example:

?
1234567 ???????@RequestMapping("hello3")???????public?String?hello3(?@RequestParam("name"?)?String?name,???????????????????????????????@RequestParam("hobby"?)?String?hobby){????????????System.?out.println("name="?+name);????????????System.?out.println("hobby="?+hobby);???????????????????return?"hello"?;??????}


but this requires that the input must have these two parameters, you can use Required=false to cancel, for example:
@RequestParam (value= "name", Required=false) String name
However, it is also possible to test the annotations completely without writing the parameters of the method to the string name, which is the same as above.
?
2) Object type:

?
123456 ??????? @RequestMapping ( "/hello4" ?) ) ??????? public ? string?hello4 (user?user) { ???????????? system.out.println ( "user.getname () =" ? +user.getname ()); ???????????? system.out.println ( "user.gethobby () =" ? +user.gethobby ()); ???????????? return ? "Hello" ?????? }


Spring MVC will press:
???? "http request parameter name =?" The property name of the command/form object "
??? rules automatically bind request data, support cascading property names, and automate basic type data conversions.
In addition, you can limit the submission method to post, that is, the @requestmapping annotation of the modified method is
@RequestMapping (value= "/hello4", Method=requestmethod.post)
?
Finally, note that if the characters presented here appear garbled, you should add the following filter to the Web. xml:
?

?
12345678910111213 <filter>???<filter-name>encodingFilter</filter-name>???<filter-class>org.springframework.web.filter.CharacterEncodingFilter?</filter-class>???<init-param>??????<param-name>encoding</param-name>??????<param-value>utf8</param-value>???</init-param></filter>??<filter-mapping>???<filter-name>encodingFilter</filter-name?>???<url-pattern>/*</url-pattern></filter-mapping>



Return data to the page several ways:????

  1. ?
    123456789 ????//返回页面参数的第二种方式,在形参中放入一个Model??????@RequestMapping(value?=?"/hello2.htm")??????public?String?hello2(int?id,Model?model){??????????System.out.println("hello2?action:"+id);??????????model.addAttribute("name",?"huangjie");??????????//这个只有值没有键的情况下,使用Object的类型作为key,String-->string??????????model.addAttribute("ok");??????????return?"hello";??????}
  1. ?
    1234567 ???//返回页面参数的第一种方式,在形参中放入一个map??????@RequestMapping(value?=?"/hello1.htm")??????public?String?hello(int?id,Map<String,Object>?map){??????????System.out.println("hello1?action:"+id);??????????map.put("name",?"huangjie");??????????return?"hello";??????}



From for notes (Wiz)

Spring MVC several ways to get path parameters-light summer's personal space-open source China community

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.