009_BeanUtils的使用
生活随笔
收集整理的這篇文章主要介紹了
009_BeanUtils的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 有時候我們的前端頁面會傳遞過來很多字段, 如果我們一個一個的獲取的話, 會顯得十分麻煩。我們可以使用一個BeanUtils的工具包把這些字段直接轉換為一個實體類。
2. 新建一個名為BeanUtilsDemo的Web工程
3. 導入BeanUtils的jar包
4. 新建一個index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>注冊頁面</title></head><body><form method="post" action="RS.action">賬號: <input type="text" name="username"><br>密碼: <input type="password" name="password"><br>郵箱: <input type="text" name="email"><br>電話: <input type="text" name="phone"><br>地址: <input type="text" name="address"><br>生日: <input type="text" name="birthday"><br><input type="submit" value="注冊"></form></body> </html>5. 新建一個UserBean.java
package com.lywgames.domain;import java.util.Date;public class UserBean {private String username;private String password;private String email;private String phone;private String address;private Date birthday;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "UserBean [username=" + username + ", password=" + password + ", email=" + email + ", phone=" + phone+ ", address=" + address + ", birthday=" + birthday + "]";}}6. 無法將String類型直接轉為Date類型, 需要轉換, 新建一個MyDateConverter.java, 實現Converter接口
package com.lywgames.util;import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.Converter;/*** 自定義 java.util.Date日期轉換器*/ public class MyDateConverter implements Converter {@Overridepublic Object convert(Class c, Object value) {String strVal = (String) value;// 將String轉換為Date --- 需要使用日期格式化DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");try {Date date = dateFormat.parse(strVal);return date;} catch (ParseException e) {e.printStackTrace();}return null;} }7. 新建一個RegisterServlet.java
package com.lywgames.servlet;import java.io.IOException; import java.util.Date; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import com.lywgames.domain.UserBean; import com.lywgames.util.MyDateConverter;public class RegisterServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 只針對post有效request.setCharacterEncoding("UTF-8");try {// 注冊自己的日期轉換器ConvertUtils.register(new MyDateConverter(), Date.class);// 轉化數據Map<String, String[]> map = request.getParameterMap();UserBean bean = new UserBean();BeanUtils.populate(bean, map);System.out.println(bean.toString());} catch (Exception e) {e.printStackTrace();}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}8. 在web.xml里配置Servlet
9. 訪問注冊頁面
10. 填寫注冊信息
11. 點擊注冊按鈕, 控制臺打印注冊信息
總結
以上是生活随笔為你收集整理的009_BeanUtils的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 008_过滤器
- 下一篇: 021_jdbc-mysql入门