JavaBean
JavaBean JavaBean属性在JSP中使用JavaBean
JavaBean的相关标签初步应用JavaBean
login.jsp
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>表单提交到JSP的页面</h4>
<form action="/day12/bean/success.jsp" method="POST">
姓名:<input type="text" name="username"/><br>
密码:<input type="password" name="password"/></br>
<input type="submit" value="登入"/>
</form>
</body>
</html>
success.jsp
<%@page import="cn.bean.user" %>
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>接受数据</h3>
<%
//获取表单的内容
String username = request.getParameter("username");
String password = request.getParameter("password");
//创建user对象,set设置值
user User = new user();
User.setUsername(username);
User.setPassword(password);
%>
<%=User.getUsername()%>
</body>
</html>
JavaBean user.java
package cn.bean;
public class user {
private String username;
private String password;
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;
}
}
使用JSP的动作标签封装数据
- <jsp:useBean>
- <jsp:setProperty>
- <jsp:getProperty>
<h3>接受数据</h3>
<%
//获取表单的内容
String username = request.getParameter("username");
String password = request.getParameter("password");
//创建user对象,set设置值
user User = new user();
User.setUsername(username);
User.setPassword(password);
%>
<jsp:useBean id="u" class="cn.bean.user"></jsp:useBean>
<jsp:setProperty property="username" name="u"/>
<jsp:setProperty property="password" name="u"/>
<jsp:getProperty property="username" name="u"/>
<jsp:getProperty property="password" name="u"/>
内省 Introspector
访问JavaBean属性的两种方式
- 直接调用bean的setXXX或gerXXX方法
- 通过内省的技术访问(java.beans包提供了内省的API)
- 内省技术是基于反射技术的
- Instrospector类获得Bean对象的BeanInfo, 然后通过BeanInfo来获取属性的描述器PropertyDescriptor, 从而获得某个属性对应的getter/setter方法,然后通过反射机制调用这些方法
/**
* 获取请求参数,完成数据
* @author limaoquan
*
*/
public class UserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**第一种
* //获取表单的内容
String username = request.getParameter("username");
String password = request.getParameter("password");
//创建user对象,set设置值
user User = new user();
User.setUsername(username);
User.setPassword(password);
*/
//第二种
//获取输入的数据
Map<String, String[]> map = request.getParameterMap();
//创建user对象
user User = new user();
//自己编写封装数据的方法
try {
populate(map,User);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(User.getUsername());
System.out.println(User.getPassword());
}
/* 完成数据的封 */
private void populate(Map<String, String[]> map, user User) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
//获取类的信息
BeanInfo info = Introspector.getBeanInfo(User.getClass());
//获取属性描述
PropertyDescriptor[] pds = info.getPropertyDescriptors();
//遍历
for(PropertyDescriptor pd : pds){
String name = pd.getName();//user中属性名称
//map的key
if(map.containsKey(name)){
//获取属性写的方法
Method m = pd.getWriteMethod();
//执行
m.invoke(User,map.get(name)[0] );
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
BeanUtils工具包
导包 commons-beanutils-1.8.3.jar commons-logging-1.1.1.jar
BeanUtils可以自动封装String,double,int
但是封装日期类型,需要自己编写代码
- 编写一个类,实现Converter接口,重写方法,把字符串转换成日期
- 在封装数据之前进行注册.
public class MyDateConverter implements Converter{
/**
* 字符串转换成日期
*/
public Object convert(Class clazz, Object obj) {
//把输入的字符串,转换成日期类型,返回
String dDate = (String) obj;
//把字符串转换为日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(dDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
public class UserBeanUtil extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取数据
Map<String, String[]> map = request.getParameterMap();
//创建user对象
user User = new user();
//在封装数据之前进行注册
MyDateConverter converter = new MyDateConverter();
ConvertUtils.register(converter, Date.class);
//完成封装
try {
BeanUtils.populate(User, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//打印
System.out.println(User.getUsername());
System.out.println(User.getPassword());
System.out.println(User.getMoney());
System.out.println(User.getBirthday());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
网友评论