表单数据自动封装到javaBean中

页面表单数据的自动封装到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里问,发我到我的邮箱也行我会尽快给你解答

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值