SpringMVC Learning Series (5): Data Binding-2

Source: Internet
Author: User

In series (4), we introduced how to bind data with @ RequestParam. Next we will look at the usage of several other data binding annotations.

1. @ PathVariable is used to bind the URL template variable value. We have introduced the usage in series (3), which is not described here.

2. @ CookieValue is used to bind data in the Cookie. Next we will use the sessionId in the Cookie for testing:

Add cookiebind action in DataBindController. The Code is as follows:

//@CookieValue Test@RequestMapping(value="/cookiebind", method = {RequestMethod.GET})public String cookieBind(HttpServletRequest request, Model model, @CookieValue(value="JSESSIONID", defaultValue="") String jsessionId){        model.addAttribute("jsessionId", jsessionId);    return "cookiebindresult";}

Add a cookiebindresult. jsp View to the views folder. The Code is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Run the test:

// @ RequestHeader Test @ RequestMapping (value = "/requestheaderbind", method = {RequestMethod. GET}) public String requestHeaderBind (HttpServletRequest request, Model model, @ RequestHeader (value = "User-Agent", defaultValue = "") String userAgent) {model. addAttribute ("userAgent", userAgent); return "requestheaderbindresult ";}

Add a requestheaderbindresult. jsp View to the views folder. The Code is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Run the test:

@ RequestMapping (value = "/modelautobind", method = {RequestMethod. POST}) public String modelAutoBind (HttpServletRequest request, Model model, AccountModel accountModel) {model. addAttribute ("accountmodel", accountModel); return "modelautobindresult ";}

With the help of @ ModelAttribute, we can simply add data to the Model and modify the above Code:

@RequestMapping(value="/modelautobind", method = {RequestMethod.POST})public String modelAutoBind(HttpServletRequest request, @ModelAttribute("accountmodel") AccountModel accountModel){        return "modelautobindresult";}

Run the test:

@ SessionAttributes (value = "sessionaccountmodel ")

Add usernamebind and passwordbind actions in DataBindController. The Code is as follows:

// @ SessionAttributes Test @ ModelAttribute ("sessionaccountmodel") public AccountModel initAccountModel () {return new AccountModel () ;}@ RequestMapping (value = "/usernamebind", method = {RequestMethod. GET}) public String userNameBind (Model model, AccountModel accountModel) {model. addAttribute ("sessionaccountmodel", new AccountModel (); return "usernamebind" ;}@ RequestMapping (value = "/usernamebind", method = {RequestMethod. POST}) public String userNameBindPost (@ ModelAttribute ("sessionaccountmodel") AccountModel accountModel) {// redirect to password binding test return "redirect: passwordbind ";} @ RequestMapping (value = "/passwordbind", method = {RequestMethod. GET}) public String passwordBind (@ ModelAttribute ("sessionaccountmodel") AccountModel accountModel) {return "passwordbind" ;}@ RequestMapping (value = "/passwordbind", method = {RequestMethod. POST}) public String passwordBindPost (@ ModelAttribute ("sessionaccountmodel") AccountModel accountModel, SessionStatus status) {// destroy the status of the object stored in @ SessionAttributes. setComplete (); // display the binding result return "sessionmodelbindresult ";}

Because @ SessionAttributes is specified on the controller, the parameter for @ ModelAttribute ("xxx") annotation will directly find the object named "xxx" in @ SessionAttributes, if not found, call the @ ModelAttribute ("xxx") annotation method to return the object and save it to @ SessionAttributes (if not found and @ ModelAttribute ("xxx ") the annotation method will throw HttpSessionRequiredException ). After the execution, you can call SessionStatus. setComplete () to destroy the stored object in @ SessionAttributes (data in HttpSession is not cleared ).

Add the usernamebind. jsp, passwordbind. jsp, and sessionmodelbindresult. jsp views in the views folder as follows:

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> <% @ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %> 
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> <% @ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %> 
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Run the test:

// @ RequestBody Test @ RequestMapping (value = "/requestbodybind", method = {RequestMethod. GET}) public String requestBodyBind (Model model) {model. addAttribute ("accountmodel", new AccountModel (); return "requestbodybind" ;}@ RequestMapping (value = "/requestbodybind", method = {RequestMethod. POST}) public @ ResponseBody AccountModel requestBodyBind (@ RequestBody AccountModel accountModel) {return accountModel ;}

Add the requestbodybind. jsp view in the views folder as follows:

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> <% @ taglib prefix = "form" uri = "http://www.springframework.org/tags/form" %> 

Run the test:

The result is correct and the conversion is successful.

7. @ RequestPart: binds the "multipart/form-data" type data. Supports javax. servlet. http. Part file upload and supports type conversion. For details, see the official documentation:

Http://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/htmlsingle/#new-in-3.1-mvc-requestpart

 

Code download: http://pan.baidu.com/s/1hqqVLTa

 

The content of the data binding part ends here.

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.