JavaBean和BeanUtils总结
程序员文章站
2022-06-24 11:00:50
JavaBeanJavaBean是一个标准的java类,用于封装数据,要求:类必须被public修饰必须提供空参的构造器成员变量必须被private修饰提供公共setter和getter方法User.javapublic class User { private String name; private int age; public String getName() { return name; } public void setN...
JavaBean
JavaBean是一个标准的java类,用于封装数据,要求:
- 类必须被public修饰
- 必须提供空参的构造器
- 成员变量必须被private修饰
- 提供公共setter和getter方法
User.java
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
BeanUtils的使用
BeanUtils工具类可以简化封装,需要导入commons-beanutils和commons-logging两个jar包。
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form method="get" action="/bean">
年龄:<input type="text" name="age"><br/>
姓名:<input type="text" name="name"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
User.java
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Test_Bean.java
@WebServlet("/bean")
public class Test_Bean extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user = new User();
//将前端传过来的参数通过request封装到一个map中
Map<String, String[]> map = req.getParameterMap();
try {
BeanUtils.populate(user, map); //将map封装到user对象里
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(user.getName() + " " + user.getAge());
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
关于属性和成员变量
需要注意的一点是,BeanUtils操纵的是属性而非成员变量,成员变量是JavaBean里的私有变量,而属性是setter和getter方法截取后的产物,例如通过getName()方法得到属性为name(去掉前面的get/set,然后首字母变小写)
以下测试代码把属性name123用BeanUtils.setProperty()方法封装到User里,BeanUtils实际调用的是User类的setName123()方法。同理通过BeanUtils.getProperty()方法实际调用的是User的getName123()
public class User {
private String name;
public String getName123() {
return name;
}
public void setName123(String name) {
this.name = name;
}
}
@WebServlet("/bean")
public class Test_Bean extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user = new User();
try {
BeanUtils.setProperty(user, "name123", 18);
String name = BeanUtils.getProperty(user, "name123");
System.out.println(name);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
本文地址:https://blog.csdn.net/Komatsu_1137/article/details/112250293