BeanUtils的作用:可以快速的将前台接收的数据封装到对象中。
一、写一个简单的Servlet程序获取表单数据
这部分可以参考这篇博客:
二、BeanUtils代码部分
首先,要使用BeanUtils,要先将其jar包导入java库
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@WebServlet("/ServletDemo2")
public class ServletDemo2 extends HttpServlet {
@Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;utf-8");
User user = new User();
/*ConvertUtils.register(new Converter() {//这是针对获取表单日期转换的转换器,用不到就可以不用写
@Override
public Object convert(Class aClass, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("conversion error");
}
String star = (String) value;
if (star.trim().equals("")) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd");
try {
if (star.contains("-")){
return sdf.parse(star);
}
if (star.contains("/")){
return sdf1.parse(star);
}
} catch (ParseException e) {
throw new RuntimeException(e);
}
return null;
}
},Date.class);*/
try {
BeanUtils.populate(user, req.getParameterMap());
System.out.println(user);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三、创建User类
import java.util.Arrays;
import java.util.Date;
public class User {
private String username;
private String password;
private String gender;
//private String age;
//private Date date;
private String[] hobbies;
private String address;
private String remark;
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 getGender() {
return gender;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public void setGender(String gender) {
this.gender = gender;
}
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
//", age='" + age + '\'' +
//", date='" + date + '\'' +
", hobbies=" + Arrays.toString(hobbies) +
", address='" + address + '\'' +
", remark='" + remark + '\'' +
'}';
}
}