自动表单数据封装到javaBean中
2011-09-18 23:04
351 查看
页面表单数据的自动封装到javaBean中
先定义一个Bean类
package com.test;
public class Bean {
private String name;
private Integer sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
}
再定义一个封装数据类用于把数据封装到Bean中
package com.test;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
/**
* 取得request对象中
* 所有的参数值并生成
* 一个相应的对象返回
* @author admin
*
*/
public class ParseHtml {
/**
*
* @param request存储着表单的HttpServletRequest对象
* @param bean要封装的表单Bean
* @return 封装好的表单Bean
*/
public Object parseRequest(HttpServletRequest request,Object bean){
//取得所有参数列表
Enumeration enum = request.getParameterNames();
//遍历所有参数列表
while(enum.hasMoreElements()){
Object obj = enum.nextElement();
try {
//取得这个参数在Bean中的数据类开
Class cls = PropertyUtils.getPropertyType(bean, obj.toString());
//把相应的数据转换成对应的数据类型
Object beanValue = ConvertUtils.convert(request.getParameter(obj.toString()), cls);
//添冲Bean值
PropertyUtils.setProperty(bean, obj.toString(), beanValue);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bean;
}
}
生成表单页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="Dispose" method="post">
<input type=text name="name"><br>
<input type=text name="sex"><br>
<input type=submit value='submit'>
</form>
</body>
</html>
定义一个Action用于处理表单传过来的数据
package com.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Dispose extends HttpServlet {
/**
* Constructor of the object.
*/
public Dispose() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用方法完成对表单数据封装到Bean中
Bean bean = (Bean)new ParseHtml().parseRequest(request, new Bean());
response.getWriter().write("name="+bean.getName()+"sex="+bean.getSex());
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
如果哪位朋友对上面的例子不明白可以发留言或直接到群1871000里问,发我到我的邮箱tianlong1569@sina.com也行我会尽快给你解答。
相关文章推荐
- 表单数据自动封装到javaBean中
- 用BeanUtils类自动封装表单数据到javabean
- Java反射机制:表单数据自动封装到JavaBean中【IT】
- 不使用BeanUtils,利用Java反射机制:表单数据自动封装到JavaBean
- 把表单的数据封装到JavaBean中
- 用BeanUtils类自动封装表单数据到javabean
- Struts2(接受表单参数)请求数据自动封装和数据类型转换
- 用BeanUtils类自动封装表单数据
- 反射的应用 将form表单的数据自动封装为对象
- ###Springmvc封装复杂表单数据:表单提交多个实体类的参数【对象类型参数,自动封装】name属性值不能重复。和实体类属性名一致
- 第八天2月28日,把表单的数据封装到JavaBean中,通过request对象,有多种方式
- 基于表单数据的封装,泛型,反射以及使用BeanUtils进行处理
- request对象多种方法封装表单数据
- Java程序员从笨鸟到菜鸟之(四十三)细谈struts2(六)获取servletAPI和封装表单数据
- ASP.NET MVC之表单集合数据自动绑定到对象属性(集合)中
- form表单数据的自动采集的js方法
- 第一百五十一节,封装库--JavaScript,表单验证--密码确认验证--回答验证--电子邮件验证加自动补全
- 用反射实现表单和Model之间互相传自动递数据
- Struts核心业务之请求数据的自动封装
- Java万能接收页面数据,并自动匹配封装为map对象