BeanUtils数据封装与表单JavaBean


一.BeanUtils工具的解释

(1)Apache的Commons组件中,提供了一个有用的工具类BeanUtils,利用它能够方便的将表单数据值填充值Bean中;

(2)javax.servlet.ServletRequest.getParameterMap()

在ServletRequest接口中,getParameter()方法的作用在于将客户端传来的参数封装在一个Map对象中,致谢参数可以通过GET POST

方法提交;

(3)org.apache.commons.beanutils.BeanUtils.populate()

这个方法的作用是将存储在Map中的参数填入给定的一个JavaBean对象中;

BeanUtils.populate(),第一个形参是一个bean对象,也就是表单的JavaBean,第二个形参是一个Map对象,也就是存储有表单圆度的Map对象;

二.举例详解

(1)编写一个JavaBean程序类RegForm

package my;
public class RegForm
{
    private String userName;
    private String password;
    private String sect;
    private String hobby[];
    private String memo;

    public void setUserName(String s)
    {
    	userName=s;
    	}
    public String getUserName()
    {
    	return userName;
    	}
    public void setPassword(String s)
    {
    	password=s;
    	}
    public String getPassword()
    {
    	return password;
    	}

   public void setSect(String s)
    {
    	sect=s;
    	}
    public String getSect()
    {
    	return sect;
    	} 

    public void setHobby(String s[])
    {
    	hobby=s;
    	}
    public String[] getHobby()
    {
    	return hobby;
    	}
     public void setMemo(String s)
    {
    	memo=s;
    	}
    public String getMemo()
    {
    	return memo;
    	}
}

(2)新建一个填写数据的表单

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<jsp:useBean id="my" class="my.RegForm" scope="page"/>
<jsp:setProperty name="my" property="*"/> //反射机制将表单自动填到‘my’表单中
<br>您的用户名是:<jsp:getProperty name="my" property="userName"/>
<br>您的口令是:  <jsp:getProperty name="my" property="password"/>
<br>您的性别是:<jsp:getProperty name="my" property="sect"/>
<br>您的爱好是:
<%
     String h[]=my.getHobby();
     if(h!=null)
     for(int i=0;i<h.length;i++)
       out.print(h[i]);
%>
<br>您的附言是:<jsp:getProperty name="my" property="userName"/>
</body>
</html>

(3)新建一个Servlet程序    reformServlet处理表单

package my;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.beanutils.*;

public class RegFormServlet extends HttpServlet
{
  protected void doGet(HttpServletRequest request,HttpServletResponse response)
                         throws ServletException,java.io.IOException
   {
      ServletContext application=getServletContext() ;
      ServletConfig config=getServletConfig() ;
      response.setContentType("text/html;charset=gb2312");
      PrintWriter out=response.getWriter();
      HttpSession session =request.getSession();
      request.setCharacterEncoding("gb2312");

      RegForm form=new RegForm();
	  Map  map=request.getParameterMap();
	  try
	  {
	  	 BeanUtils.populate(form,map);
	  }catch(Exception e)
	  {
	  	System.out.println("表单处理出错:"+e);
	  	}

    out.print("<br>您的姓名是:"+form.getUserName());
    out.print("<br>您的口令是:"+form.getPassword());
    out.print("<br>您的性别是:"+form.getSect());
    out.print("<br>您的爱好是:");
    String h[]=form.getHobby();
    if(h!=null)
     for(int i=0;i<h.length;i++)
       out.print(h[i]);
    out.print("<br>您的附言是:"+form.getMemo());
  }	

protected void doPost(HttpServletRequest request,HttpServletResponse response)
                                throws ServletException,java.io.IOException
  {
  	   doGet(request,response);
  	}
}

(4)还有一个创建数据的界面

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="exam.jsp">
  <p>会员注册信息</p>
  <p>用户名:
    <label>
    <input name="userName" type="text" id="userName" />
    </label>
</p>
  <p>口令:
    <label>
    <input name="password" type="password" id="password" />
    </label>
  </p>
  <p>性别:
    <label>
    <input name="sect" type="radio" value="男" checked="checked" />
    </label>
  男
  <label>
  <input type="radio" name="sect" value="女" />
  </label>
  女</p>
  <p>爱好:
    <label>
    <input name="hobby" type="checkbox" id="hobby" value="篮球" />
    </label>
  篮球
  <label>
  <input name="hobby" type="checkbox" id="hobby" value="排球" />
  </label>
  排球
  <label>
  <input name="hobby" type="checkbox" id="hobby" value="足球" />
  </label>
  足球</p>
  <p>附言:
    <label>
    <textarea name="memo" id="memo"></textarea>
    </label>
    <label>
    <input type="submit" name="Submit" value="提交" />
    </label>
  </p>
</form>
</body>
</html>

(5)关于servlet的部署省去过程

(6)ok!

时间: 2024-08-09 17:04:59

BeanUtils数据封装与表单JavaBean的相关文章

MVC扩展ModelBinder,通过继承DefaultModelBinder把表单数据封装成类作为action参数

把视图省.市.街道表单数据,封装成一个类,作为action参数.如下: action方法参数类型: namespace MvcApplication1.Models{    public class Customer    {        public string Address { get; set; }    }} 在自定义ModelBinder中,接收视图表单数据,封装成Customer类. using System.Web; using System.Web.Mvc; using M

springmvc文件上传及表单数据封装

补充: form表单需要提交时间,springmvc封装到实体类的Date字段时,丢失时分秒,可以在controller中添加     @InitBinder     public void initBinder(WebDataBinder binder) {         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         dateFormat.setLenient(f

JavaBean+jsp开发模式 --结合form表单 实例

1.创建form表单 <%@ 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&qu

SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换

SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换 场景一:表单中的日期字符串和JavaBean的Date类型的转换 在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换, 例如,如下代码S_ROLE_QO这个bean中包含有Date类型属性,不处理就拿不到值. /** * 分页取角色列表 * @param s_ROLE_QO 角色查询条件 * @return */ @RequestMapping(value = "/

异步发送表单数据到JavaBean,并响应JSON文本返回

1)  提交表单后,将JavaBean信息以JSON文本形式返回到浏览器 <form> 编号:<input type="text" name="id" value="1"/><br/> 姓名:<input type="text" name="name" value="哈哈"/><br/> 薪水:<input type=&q

struts2 获取表单数据封装到list和map集合

一.获取封装表单数据到list集合 示例 获取用户输入的用户名和密码并输出用户名. jsp页面 list[0]表示list中的第一个user对象 Java代码 二.封装表单数据到map集合 示例 获取用户输入的用户名和密码并输出用户名. jsp页面 'one'是map的key通过key来获取user对象 java代码

把表单数据封装成json格式 插件可用

来源:https://github.com/macek/jquery-serialize-object 请看 文件里面的jquery-serialize-object-master.zip 有一个文件:jquery.serialize-object.js , 是用来 把表单 数据  进行JSON 格式. 用法: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&q

封装表单案例

将页面的数据封装到表单中 register.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+

在jsp提交表单的参数封装到一个方法里

建议去看一下孤傲苍狼写的Servlet+JSP+JavaBean开发模式(http://www.cnblogs.com/xdp-gacl/p/3902537.html), 最好把他JavaWeb学习总结全部看完会有很大的收获哦! 而把jsp专递的参数封装到一个方法里面也是从他那里学到的. 我觉得特别有用,尤其是在做项目的时候能省很多的代码  一: 需要的包 根据上一篇JDBC+Servlet+jsp(http://www.cnblogs.com/zhu520/p/6913650.html)的内容