SpringMVC (3) HTTP request binding data

Annotation for binding parameters

 

@RequestParam

bind request parameters

@CookieValue

bind cookie value

@RequestHeader

Bind request header parameters

PathVariable

Bind variables in URL

Example

 

// bind request parameters
@RequestMapping(value="/index")
public ModelAndView index(@RequestParam("sex") String sex,
						  @RequestParam("age") String age) {
	System.out.println(sex);
	System.out.println(age);
	// return the specified jsp page  
	ModelAndView mav = new ModelAndView("message");
	return mav;
}
@RequestParam has the following three parameters

 

1.value: parameter name

2.required: Whether it is required, the default is true , indicating that the request must contain the corresponding parameter name, if it does not exist, an exception will be thrown

@RequestParam(value = "sex" , required = false ) String sex // If not required, it must be specified as false

3.defaultValue: default parameter name, when setting this parameter, required is automatically set to false . This parameter is required in rare cases and is not recommended .

 

 

// bind cookies, request header parameters
@RequestMapping(value="/index")
public ModelAndView index(@CookieValue("JSESSIONID") String sessionId,
						  @RequestHeader("Accept-Language") String acceptlanguage) {
	System.out.println(sessionId);
	System.out.println(acceptlanguage);
	// return the specified jsp page  
	ModelAndView mav = new ModelAndView("message");
	return mav;
}
 

 

 

// bind the variable in the URL
@RequestMapping(value="{userId}",method=RequestMethod.POST)
public ModelAndView index(@PathVariable("userId")String userId) {
	System.out.println(userId);
	// return the specified jsp page  
	ModelAndView mav = new ModelAndView("message");
	return mav;

}
 

 

 

---------------------------------------------------------------------------------------------------------------------------------------------------------

 

Use command / form object binding

 

@RequestMapping(value="{userId}",method=RequestMethod.POST)
public ModelAndView index(User user) {
	System.out.println(user.userId);
	// return the specified jsp page  
	ModelAndView mav = new ModelAndView("message");
	return mav;
}

//Use Servlet API object as input parameter
@RequestMapping(value="/index")
public void index(HttpServletRequest request,
							HttpServletResponse response,
							HttpSession session) throws ServletException, IOException {
	request.getRequestDispatcher("message.jsp").forward(request, response);

}
  Note: If the processing method uses httpServletResponse to return the response, the return value of the processing method can be set to void .

 

 

// Use Spring's Servlet API proxy class
public void index(WebRequest request){
 
}
 
Use IO objects as input parameters
public void index(OutputStream os){
		
}
 

 

 

In addition, java.util.Locale and java.security.Principal are also supported , and the corresponding values ​​can be obtained through getLocale() and getUserPrincipal() of Serlvet 's httpServletRequest . If the input parameter type of the processing method is locale or principal, Spring MVC automatically obtains the corresponding object from the request object and passes it to the input parameter of the processing method.

 

 

return json data format

1. Import the jar package

jackson-core-asl-1.9.13.jar

 

jackson-mapper-asl-1.9.13.jar

 

2. Add XML configuration

 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json</value>
                            <value>text/json</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
 

 

3. Code

 

@RequestMapping(value="/index")
public ResponseEntity<User> index(){
	User user = new User();
	user.setAge("A");
	user.setSex("B");
	return new ResponseEntity<User>(user,HttpStatus.OK);

}
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326081985&siteId=291194637