通常情况下,我们会选择使用JavaBean来存储内存中的数据,除非是非常简单的数据没有必要多编写一个JavaBean类的时候才会选择使用Map进行存储。
优点:
缺点:
优点:
缺点:
代码量增多,需要花时间去封装JavaBean类
BeanUtils是由Apache公司所开发的工具包,目的是为了简化数据封装,方便Java程序员对JavaBean类进行简便的操作。
BeanUtils的作用
将Map中的数据填充到JavaBean对象中
API方法介绍
BeanUtils.populate(JavaBean对象,map对象);
使用步骤
Index.html代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> </head> <body> <form action="/class12_beanUtils/beanUtilsDemo" method="post"> 用户名<input type="text" name="username"/><br/> 密码<input type="password" name="password"/><br/> 邮箱<input type="text" name="email"/><br/> 手机号<input type="text" name="phone"/><br/> 地址<input type="text" name="address"/><br/> 兴趣爱好 <input type="checkbox" name="hobby" value="basketball"/>篮球 <input type="checkbox" name="hobby" value="football"/>足球 <input type="checkbox" name="hobby" value="yumaoball"/>羽毛球 <input type="checkbox" name="hobby" value="pingpangball"/>乒乓球<br/> <button>提交</button> </form> </body> </html>
JavaBean代码
import java.util.Arrays; public class User { private String username; private String password; private String email; private String phone; private String address; private String[] hobby; public User() { } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", phone='" + phone + '\'' + ", address='" + address + '\'' + ", hobby=" + Arrays.toString(hobby) + '}'; } 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 getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String[] getHobby() { return hobby; } public void setHobby(String[] hobby) { this.hobby = hobby; } public User(String username, String password, String email, String phone, String address, String[] hobby) { this.username = username; this.password = password; this.email = email; this.phone = phone; this.address = address; this.hobby = hobby; } }
Servlet代码
package com.drimwai.servlet; import com.drimwai.servlet.bean.User; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Map; public class BeanUtilsDemo extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //一次性获取所有的请求参数,并且将所有的请求参数封装到Map中,请求参数名就是map的key,请求参数值就是map的value Map<String, String[]> parameterMap = request.getParame terMap(); //将请求参数封装到User对象中: 将parameterMap中的数据存到User对象中 User user = new User(); //使用BeanUtils将Map中的数据存储到JavaBean对象中 try { //在使用BeanUtils将Map中的数据存储到JavaBean的时候,会自动进行一些基本的类型转换 //要求:JavaBean中的属性名和Map中的key要对应,才能封装数据,如果Map中的key和JavaBean的属性名不对应那么这个属性就无法封装 BeanUtils.populate(user,parameterMap); } catch (Exception e) { e.printStackTrace(); } System.out.println(user); } }
创作不易!欢迎点赞/评论!!