前端学习笔记:BeanUtils工具类的使用

前端学习笔记:BeanUtils工具类的使用

BeanUtils的用法

====
BeanUtils的API docs:
https://docs.spring.io/spring/docs/current/javadoc-api/index.html?index-files/index-1.html

BeanUtils的作用:可以将jsp页面传过来的多个参数封装成一个对象。

一、搭建数据库

drop database if exists `pcd01`;
CREATE DATABASE IF NOT EXISTS `pcd01` CHARACTER SET `utf8` COLLATE `utf8_bin`;
USE `pcd01`;


DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user`(
  `id` INT PRIMARY KEY AUTO_INCREMENT,
  `username` VARCHAR(50) DEFAULT NULL,
  `password` VARCHAR(50) DEFAULT NULL,
  `nickname` VARCHAR(50) DEFAULT NULL
);

INSERT INTO `user` VALUES(NULL, "zhangsan", "123456", "张三");
INSERT INTO `user` VALUES(NULL, "lisi", "123456", "李四");
INSERT INTO `user` VALUES(NULL, "wangwu", "123456", "王五");


二、搭建项目
1. 创建工程:Create New Project-->Empty Project, Project name=exercises01, Finish
2. 创建项目:New-->Module-->Java Enterprise, Java EE version=Java EE 6, Additional Libraries and Frameworks=Web Application(3.0), Versions=3.0, 选中Create web.xml-->Next, Module name=exercise01
3. 在src文件夹下建立层级
servlet包:com.itcast.servlet
4. 导入jar包
在web/WEB-INF文件夹下建立文件夹lib,将jar包放入lib文件夹下,add as library, 所需要的jar包有:
BeanUtils的:commons-beanutils-1.9.4.jar, commons-logging-1.2.jar
5.配置tomcat
(1)Run-->Edit Configuration, 点击+,选Tomcat Server-->Local
(2)Name=tomcat8, 在Server标签页中,点击Configure按钮,Tomcat Home选择tomcat的安装目录,libraries选择tomcat安装目录下的lib文件夹,加入jsp-api.jar和servlet-api.jar
(3)在Deployment标签页中,Application context=/项目名称,即:Application context=/exercise01


三、核心代码

//file name: LoginServlet.java
package com.itcast.web;

import com.itcast.domain.User;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //方式一:我们自己手动封装User对象
        /*
        //1.接收浏览器提交的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2.将其封装成User对象  //目的:为了给大家讲解BeanUtils工具类的使用
        User user = new User(0, username, password, "");
        System.out.println(user);
        //3.调用Service层的功能,根据封装后的对象,查询是否有该对象
        //4.判断,如果存在,重定向到index.jsp
        //5.判断,如果不存在,请求转发到login.jsp登陆页面
        */

        //方式二:通过BeanUtils工具类实现,自动将数据封装成User对象
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, map);
            System.out.println(user);//从前端页面得到的结果:User{id=null, username='zhangsan', password='123456', nickname='张三'}
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}

全部代码和笔记已经以资源的形式上传到CSDN,搜索关键字为:BeanUtils工具类的使用.zip

链接为:https://download.csdn.net/download/elizabethxxy/11953187

发布了106 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/elizabethxxy/article/details/102876279
今日推荐