Detailed SpringMVC began to learn the RequestMapping

In this paper, a simple small example, briefly SpringMVC development RequestMapping related applications, only to learn to share, if any inadequacies, please correct me.

What is RequestMapping?

RequestMapping is a process for annotation request address mapping, it can be used for class or method. For the class, all the methods in response to the request class are represented in the address as the parent path.

A common use of RequestMapping

1. RequestMapping defaults

As follows: The default attribute value, given default mapping path i.e.

1  / * 
2       * default values, i.e. values corresponding value
 . 3       * @RequestMapping ( "Mapping01")
 . 4       * @return 
. 5       * / 
. 6      @RequestMapping ( "/ Mapping01" )
 . 7      public ModelAndView Mapping01 () {
 . 8          the System. out.println ( "Mapping01 been called ...." );
 9          ModelAndView MAV = new new ModelAndView ();
 10          mav.addObject ( "msg", "the HelloWorld, HelloSpringMVC !!!" );
 11          // mav.setViewName ( "the WEB-INF / JSPs / The HelloWorld.jsp"); 
12 is          mav.setViewName ( "the HelloWorld");
13         return mav;
14     }

2. ReqeustMapping of property value and method

value represents the path map, is an array type, a plurality of paths can be mapped, method represents a request distal manner, as follows: two paths represent accessible, and the support GET, POST accessed in two ways.

. 1  / ** 
2       * value is an array, can specify a different path
 . 3       * Method is an array, can specify a different access mode
 . 4       * @return 
. 5       * / 
. 6      @RequestMapping (value = { "/ Mapping02", "Mapping0B "}, Method = {RequestMethod.GET, RequestMethod.POST})
 . 7      public ModelAndView Mapping02 () {
 . 8          System.out.println (" Mapping02 been called .... " );
 . 9          ModelAndView MAV = new new ModelAndView ();
 10          mav.addObject ( "MSG", "the HelloWorld, HelloSpringMVC !!!" );
 . 11          // mav.setViewName ( "the WEB-INF / JSPs / The HelloWorld.jsp");
12         mav.setViewName("HelloWorld");
13         return mav;
14     }

3. RequestMapping in params

params parameter indicative of access that can be obtained as follows: GET expressed support for access to, and must have parameters can uname

1  / * 
2       * access path specified value, method specified access manner GET method must be
 3       *, and must have a parameter the uname
 . 4       * @return 
. 5       * / 
. 6      @RequestMapping (value = "/ Mapping03", method = RequestMethod.GET, the params = "the uname" )
 . 7      public ModelAndView Mapping03 (String the uname) {
 . 8          System.out.println ( "Mapping03 been called ...." );
 . 9          ModelAndView MAV = new new ModelAndView ();
 10          MAV. addObject ( "MSG", the uname + ">>>>> the HelloWorld, HelloSpringMVC !!!" );
 . 11          // mav.setViewName ( "the WEB-INF / JSPs / the HelloWorld.jsp");
12         mav.setViewName("HelloWorld");
13         return mav;
14     }

As follows: There must be represented in the access path and uage uname two parameters, and uage must be equal to 13 can be accessed

1  / * 
2       * access path specified value, method specified access is to be made GET method
 3       * parameter and must have a uname, and uage, and must equal uage 13 is
 . 4       * @return 
. 5       * / 
. 6      @RequestMapping ( = value "/ Mapping04", Method = RequestMethod.GET, the params = { "the uname", "uage = 13 is" })
 . 7      public ModelAndView Mapping04 (String the uname, int uage) {
 . 8          System.out.println ( "is called Mapping04 a .... " );
 . 9          System.out.println (" the uname = "+ the uname +", uage = "+ uage);
 10          ModelAndView MAV = new new ModelAndView ();
 . 11         mav.addObject("msg", uname+">>>>>HelloWorld,HelloSpringMVC!!!");
12         //mav.setViewName("WEB-INF/jsps/HelloWorld.jsp");
13         mav.setViewName("HelloWorld");
14         return mav;
15     }

The headers 4. RequestMapping

headers represent header information requesting access must be met in the manner specified can access. As follows:

. 1  / ** 
2       * headers: requesting access to the specified Header information, it must meet the specified manner can be accessed
 . 3       * Consumes: specifying process submission type (Content-Type) request, for example, application / json, text / html ;
 . 4       * @return 
. 5       * / 
. 6      @RequestMapping (value = "/ Mapping05", headers = "text = the Accept / HTML, file application / XHTML + XML, file application / XML;" )
 . 7      public ModelAndView Mapping05 () {
 . 8          the System. out.println ( "Mapping05 been called ...." );
 9          ModelAndView MAV = new new ModelAndView ();
 10          mav.addObject ( "msg", ">>>>> the HelloWorld, HelloSpringMVC !!!" );
11         //mav.setViewName("WEB-INF/jsps/HelloWorld.jsp");
12         mav.setViewName("HelloWorld");
13         return mav;
14     }

 5. RequestMapping produces in

represents produces returned Content-Type, as follows: Content-Type returned showing application / json format

1  / * 
2       * REQUEST request method handles only the Accept request comprises "application / json" in the header,
 3       * specify the content type (Content-Type) is returned file application / JSON;
 . 4       * @return 
. 5       * / 
. 6      @RequestMapping (value = "/ Mapping06", Produces = "file application / JSON" )
 . 7      @ResponseBody
 . 8      public String [] Mapping06 () {
 . 9          System.out.println ( "JSON is called ....." );
 10          String [] = ARR new new String [] { "Bob", "flower" };
 . 11          return ARR;
 12 is      }

Restful way to get value

Extracting parameters based on the path must be increased @PathVariable ( "uname") identified in the parameter

As follows:

 1 /**
 2      * 采用Restful方式传递参数
 3      * @param name
 4      * @return
 5      */
 6     @RequestMapping("/Mapping07/{uname}")
 7     public String Mapping07(@PathVariable("uname") String name,Model model){
 8         System.out.println("uname="+name);
 9         model.addAttribute("msg",  name+ ">>>>>HelloWorld,HelloSpringMVC!!!");
10         return "HelloWorld";
11     }

Access as shown below:

Further, the present embodiment is used in view resolver needs to be configured, as shown below:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 xmlns:context="http://www.springframework.org/schema/context"
 5 xmlns:mvc="http://www.springframework.org/schema/mvc"
 6 xsi:schemaLocation="http://www.springframework.org/schema/beans
 7  http://www.springframework.org/schema/beans/spring-beans.xsd
 8  http://www.springframework.org/schema/context
 9 http://www.springframework.org/schema/context/spring-context.xsd
 10  http://www.springframework.org/schema/mvc
 . 11  http://www.springframework.org/schema/mvc/spring- mvc.xsd " > 
12 is        ! <- configuration annotations scan -> 
13 is       < context: scan-Component Base-Package =" com.hex.second " > </ context: Component-scan > 
14       ! <- support MVC notes -> 
15       < MVC: annotation-Driven > </ MVC: annotation-Driven > 
16       ! <- static resources -> 
17       < MVC:default-servlet-handler/>
18       <!-- 配置视图解析器 -->
19     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
20     id="internalResourceViewResolver">
21         <property name="prefix" value="/WEB-INF/jsps/"/>
22         <property name="suffix" value=".jsp"/>
23     </bean>
24 </beans>

Remark

Chiaki without stunning, beauty is pleasing to the eye.

Guess you like

Origin www.cnblogs.com/hsiang/p/11373629.html