一.BeanUtils工具的解释

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

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

在ServletRequest接口中,getParameter()方法的作用在于将client传来的參数封装在一个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>您的username是:<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>username:
<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!

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

  1. JavaWeb -- Struts1 使用示例: 表单校验 防表单重复提交 表单数据封装到实体

    1. struts 工作流程图 超链接 2. 入门案例 struts入门案例: 1.写一个注册页面,把请求交给 struts处理 <form action="${pageContext ...

  2. 利用BeanUtils工具类封装表单数据

    一.BeanUtils工具类的使用 1.首先导入BeanUtils工具类的jar包 commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar 2.se ...

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

    1.创建form表单 <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...

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

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

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

    SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换 场景一:表单中的日期字符串和JavaBean的Date类型的转换 在使用SpringMVC的时候,经常会遇到表单中的 ...

  6. Struts2中将表单数据封装到List和Map集合中

    一.将表单数据封装到Map集合中 1.创建MapAction类 import cn.entity.User; import com.opensymphony.xwork2.ActionSupport; ...

  7. 纯js将form表单的数据封装成json 以便于ajax发送

    使用方式: var json = form2Json("formId");//这里的参数是form表单的id值 form2json.js function form2Json(fo ...

  8. form表单数据封装成json格式并提交给服务器

    1.jsp代码,form表单: <form action="#" id="costForm"> <input type="hidde ...

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

    把视图省.市.街道表单数据,封装成一个类,作为action参数.如下: action方法参数类型: namespace MvcApplication1.Models{    public class ...

随机推荐

  1. android http同步请求

    1.界面 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...

  2. 五子棋Web版的开发(二)--整合Spring4.3+hibernate4+Struts2.3

    拖了这么久才把ssh框架给整合完毕,期间发现自己对SSH的知识真的是知之甚少.在整合期间遇到了无数的坑,我还是先把项目地址发一下吧 首先我遇到的第一个问题是 CreateQuery is not va ...

  3. Eclipse+ADT的环境搭建

    Index: . Java环境变量的设置 . Android环境变量的设置 1.Java环境变量的设置 A.属性名称:JAVA_HOME 属性值:C:\Program Files\Java\jdk1. ...

  4. 有意思的C宏

    在Linux内核.嵌入式代码等传统的C代码里,会有一些难以识别的宏定义.我记得在eCos, UBoot, FFmpeg有一些比较BT的宏定义,很难读懂.对于C++程序员来说,最好将这种难读的宏定义转成 ...

  5. $.ajax和vue-resource实现OAuth

    Vue.js——使用$.ajax和vue-resource实现OAuth的注册.登录.注销和API调用 概述 上一篇我们介绍了如何使用vue resource处理HTTP请求,结合服务端的REST A ...

  6. 一劳永逸让windows 64位操作系统 禁止强制驱动签名

    如何让WINDOWS7 64位直接加载“禁用强制驱动程序签名”方式启动  Windows Client 论坛 > Windows 7 问题 0 登录进行投票 因为开发需要,要装一台设备的驱动,但 ...

  7. BZOJ 1087 互不侵犯King (位运算)

    题解:首先,这道题可以用位运算来表示每一行的状态,同八皇后的搜索方法,然后对于限制条件不相互攻击,则只需将新加入的一行左右移动与上一行相&,若是0则互不攻击,方案可行.对于每种方案,则用递推来 ...

  8. [置顶] 局部加权回归、最小二乘的概率解释、逻辑斯蒂回归、感知器算法——斯坦福ML公开课笔记3

    转载请注明:http://blog.csdn.net/xinzhangyanxiang/article/details/9113681 最近在看Ng的机器学习公开课,Ng的讲法循循善诱,感觉提高了不少 ...

  9. :before :after

    #p1:before{ content: "哈哈哈 "; color: red;}#p1:after{ content: "哈哈哈"; color: #452d ...

  10. Knockout快速扫盲

    1.Knockout.js是什么?   Knockout是一款很优秀的js库,它可以快速让你使用一个干净整洁的底层数据模型,即可创建一个富文本且具有良好显示和编辑功能的用户界面.任何时候你的ui需要自 ...