Spring's data binding framework for understanding

Reference video practice: Portal

 

Experimental objective:

  (1) map URL placeholder binding to the reference method used @pathVarible

(2) bind request parameters to the parameter control method used @RequestParam

(3) The request form is bound parameter controller Method

(4) The control method of binding request parameters Map object

 

 

(1) map URL placeholder binding to the reference method used @pathVarible:

usage:

 

2) binding request parameters to the parameter control method used @RequestParam

step:

At the reference method used @RequestParam annotation controller may request parameters passed to the method, by @RequestParam annotation attribute specifies the name of the parameter value, required attribute must specify whether the parameter, the default is true, indicating that the request must include a parameter corresponding to the parameter , if not, an exception is thrown.

In the method of adding requestParam SpringMVCHandler class, a binding request @RequestParam annotation method parameter to a parameter controller. as the picture shows.

Add the index.jsp page in a "Request Param" hyperlink, as shown below: <a href="springmvc/requestParam?userName=my&password=123456"> Request Param </a>

Browse page index.jsp, click the "Request Param" link, console output "Request Param: my 123456".

index.jsp:

	<a href="springmvc/requestParam?username=my&password=123456">Request Param</a>

SpringMVCHandler.java:

@RequestMapping("/requestParam")
	public String requestParam(@RequestParam(value="username")String userName,@RequestParam(value="password")String passWord)
	{
		System.out.println("request Param:"+userName+" "+passWord);
		return "success";
	}

 

(3) The request form is bound parameter controller Method

step:

 

Spring MVC will automatically match according to the parameters and attribute names, attribute values ​​automatically populated for the object, and support the cascade. First, create a package com.springmvc.entity in the src directory of the project, to create entity classes UserInfo.java and Address.java in the package.

The method then added in SpringMVCHandler saveUserInfo class, the request parameters to the controller bind process of UserInfo form objects. as the picture shows.

Finally, create a form in index.jsp page, as shown.

Browse page index.jsp, enter your user name "my", password "123456" provinces "JiangSu" and the city "NanJing" in the form, click "Submit" button, the console output is as follows: UserInfo [userName = my, password = 123456, favorate = null, address = address [province = JiangSu, city = NanJing]

 

Create a new class:

package com.springmvc.entity;

public class UserInfo {
	private String username;
	private String password;
	private String favorite;
	private Address address;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getFavorite() {
		return favorite;
	}
	public void setFavorite(String favorite) {
		this.favorite = favorite;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public UserInfo(String username, String password, String favorite, Address address) {
	
		this.username = username;
		this.password = password;
		this.favorite = favorite;
		this.address = address;
	}
	public UserInfo() {
		super();
	}
	@Override
	public String toString() {
		return "UserInfo [username=" + username + ", password=" + password + ", favorite=" + favorite + ", address="
				+ address + "]";
	}
	

	
}

 

And address classes:

package com.springmvc.entity;

public class Address {
	private String province;
	private String city;
	public Address(String province, String city) {
	
		this.province = province;
		this.city = city;
	}
	@Override
	public String toString() {
		return "Address [province=" + province + ", city=" + city + "]";
	}
	public String getCity() {
		return city;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public Address() {
		super();
	}
}

 

index.jsp

<form action="springmvc/saveUserInfo" method="post"><!-- 表单提交 -->
		username:<input type="text" name="username"><br>
		password:<input type="text" name="password"><br>
		province:<input type="text" name="address.province"><br>
		city:<input type="text" name="address.city"><br>
		<input type="submit" value="提交"><br>
	</form>

Screenshot results:

 

4) binding request parameters to target control method Map

step:

Spring MVC annotation can pass the form data to the controller type Map method of ginseng into the created class com.springmvc.entity UserInfoMap package, define Map <String, UserInfo> UIMap attribute type, and provide for the property get and set methods, the code shown in FIG.

Add SpringMVCHandler class method getUserInfos, to achieve the Map request parameters bound to the controller method, and traverse Map, the content of the Map output to the console. as the picture shows.

Index.jsp page in creating a form as shown in FIG.

Browse page index.jsp, enter information in the form of two users, respectively. The first user's user name "my", password "123456" provinces "JiangSu" and the city "NanJing"; the first user's username "sj", password "123456" provinces "JiangSu" and the city "YangZhou . " Click "Submit" button, console output as shown.

 

 

Create a Map class, the key is of type String, key-value pair is userInfo class

package com.springmvc.entity;

import java.util.Map;

public class UserInfoMap {
	
	private Map<String,UserInfo> uiMap;

	public Map<String, UserInfo> getUiMap() {
		return uiMap;
	}

	public void setUiMap(Map<String, UserInfo> uiMap) {
		this.uiMap = uiMap;
	}
	
}

 

index.jsp in the wording:

<form action="springmvc/getUserInfo" method="post">
		username1:<input type="text" name="uiMap['u1'].username"><br>
		password1:<input type="text" name="uiMap['u1'].password"><br>
		province1:<input type="text" name="uiMap['u1'].address.province"><br>
		city1:<input type="text" name="uiMap['u1'].address.city"><br>
		
		username2:<input type="text" name="uiMap['u2'].username"><br>
		password2:<input type="text" name="uiMap['u2'].password"><br>
		province2:<input type="text" name="uiMap['u2'].address.province"><br>
		city2:<input type="text" name="uiMap['u2'].address.city"><br>
		
		<input type="submit" value="提交"><br>
	</form>

 

The results:

 

 

Published 499 original articles · won praise 67 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_41286356/article/details/105082425