您的位置:首页 > 编程语言 > Java开发

自动表单数据封装到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也行我会尽快给你解答。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息