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> 來自:http://blog.csdn.net/xlgen157387/article/details/39346121 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!