表单数据自动封装到javaBean中

2023-11-04

页面表单数据的自动封装到javaBean中

先定义一个Bean类

package com.test;

public class Bean {
private String name;
private Integer sex;
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
public Integer getSex() {
   return sex;
}
public void setSex(Integer sex) {
   this.sex = sex;
}


}

再定义一个封装数据类用于把数据封装到Bean中


package com.test;

import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
/**
* 取得request对象中
* 所有的参数值并生成
* 一个相应的对象返回
* @author admin
*
*/
public class ParseHtml {
/**
*
* @param request存储着表单的HttpServletRequest对象
* @param bean要封装的表单Bean
* @return 封装好的表单Bean
*/
public Object parseRequest(HttpServletRequest request,Object bean){
   //取得所有参数列表
   Enumeration enum = request.getParameterNames();
   //遍历所有参数列表
   while(enum.hasMoreElements()){
   
    Object obj = enum.nextElement();
    try {
     //取得这个参数在Bean中的数据类开
     Class cls = PropertyUtils.getPropertyType(bean, obj.toString());
     //把相应的数据转换成对应的数据类型
     Object beanValue = ConvertUtils.convert(request.getParameter(obj.toString()), cls);
     //添冲Bean值
     PropertyUtils.setProperty(bean, obj.toString(), beanValue);
    } catch (IllegalAccessException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (InvocationTargetException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (NoSuchMethodException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  
   return bean;
}
}


生成表单页面

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
    <form action="Dispose" method="post">
    <input type=text name="name"><br>
    <input type=text name="sex"><br>
    <input type=submit value='submit'>
    </form>
</body>
</html>


定义一个Action用于处理表单传过来的数据


package com.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Dispose extends HttpServlet {

/**
* Constructor of the object.
*/
public Dispose() {
   super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
   super.destroy(); // Just puts "destroy" string in log
   // Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //调用方法完成对表单数据封装到Bean中
    Bean bean = (Bean)new ParseHtml().parseRequest(request, new Bean());
    response.getWriter().write("name="+bean.getName()+"sex="+bean.getSex());
   
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
   // Put your code here
}

}

如果哪位朋友对上面的例子不明白可以发留言或直接到群1871000里问,发我到我的邮箱也行我会尽快给你解答

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

表单数据自动封装到javaBean中 的相关文章

随机推荐

  • 前端项目部署到服务器的几种方式

    1 使用github作为服务器 在github创建一个特殊的仓库 仓库名是你的 用户名 github io 这样里面放入你的打包文件就可以作为服务器访问 访问网址为http 用户名 github io 文件名 2 使用netlify服务器
  • Linux下Tomcat设置404错误页无法正确定向

    使用自定义错误页面代替tomcat中的默认错误页面 我的配置如下默认配置文件 opt tomcat5 conf web xml程序配置文件 opt tomcat5 webapps 本公司的WEB WEB INF web xml 在web x
  • 【JavaScript内置对象】Date对象,从零开始

    JavaScript内置对象 Date对象 从零开始 时间的表示方式 时间表示的基本概念 最初 人们是通过观察太阳的位置来决定时间的 但是这种方式有一个最大的弊端就是不同区域位置大家使用的时间是不一致的 相互之间没有办法通过一个统一的时间来
  • 数据结构:线性表理论题目集

    大一下半期数据结构 数据结构 第2章 线性表 选择题 1 下述哪一条是顺序存储结构的优点 北方交通大学 2001 一 4 2分 A 存储密度大 B 插入运算方便 C 删除运算方便 D 可方便地用于各种逻辑结构的存储表示 2 下面关于线性表的
  • Java 中数据结构HashMap的用法

    Java HashMap HashMap 是一个散列表 它存储的内容是键值对 key value 映射 HashMap 实现了 Map 接口 根据键的 HashCode 值存储数据 具有很快的访问速度 最多允许一条记录的键为 null 不支
  • linux上运行gfortran,linux – gfortran:在64位系统中编译32位可执行文件

    我在AMD Athlon tm 64 X2双核处理器5200 2上运行Ubuntu 12 10 64位 Linux内核3 5 0 51通用 我有GNU Fortran Ubuntu Linaro 4 7 2 2ubuntu1 4 7 2 我
  • 数字三角形(C语言)

    一 问题描述 给定一个由行数字组成的数字三角形 试着设计一个算法 计算出从三角形的顶到底的一条路径 使得该路径经过的数字总和最大 并分析算法的计算复杂性 如下图所示 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 二 问题分析及
  • Adam优化算法(Adam optimization algorithm)

    Adam优化算法 Adam optimization algorithm Adam优化算法基本上就是将Momentum和RMSprop结合在一起 初始化 2 在第t次迭代中 用mini batch梯度下降法计算出dw和db 3 计算Mome
  • mysql时间区间效率_对于sql中使用to_timestamp判断时间区间和不使用的效率对比及结论...

    关于日期函数TO TIMESTAMP 拓展 date类型是Oracle常用的日期型变量 时间间隔是秒 两个日期型相减得到是两个时间的间隔 注意单位是 天 timestamp是DATE类型的扩展 可以精确到小数秒 fractional sec
  • 打卡湘大OJ第一天

    1063 输入与输出1 Description 请输入一个非负整数 输出其10进制和16进制 使用A F 的值 中间用逗号隔开 不要输出换行 Sample Input 15 Sample Output 15 F 题解 include
  • 听说你还不会写通讯录?C语言通讯录#存储到文件里去,动态版

    前言 会写的不一定真的懂了 本篇文章结合了B站鹏哥的写作手法 循序渐进推进知识点 一步步教你写代码 非常适合初学者学习 建议反复观看 同时作者我也是一个C语言B站初学者 欢迎一起学习交流 批评指正 家人们 来都来了 动动你们发财的小手 给我
  • git指令

    拉取远程分支 git checkout track origin dev git reset soft a0ad996d7b797745c9bdc93e2de6d1bc30ddce8b 一 Git 常用命令速查 git branch 查看本
  • 如何在十分钟内插入1亿条记录到Oracle数据库?

    这里提供一种方法 使用 APPEND 提示 使得十分钟内插入上亿数据成为可能 Create table create table TMP TEST CHAS LEE f01 VARCHAR2 20 f02 NUMBER 10 not nul
  • XSS-Game level 5

    第五关过滤了
  • Java学完SSM后很迷茫,接下来该学哪些呢?

    没必要跟着别人的学习路线图走 你要清楚你的目的是找工作 写过很多次大学的四年安排 今天这篇专门写给大四即将找工作的同学 一 找什么工作 首先分析自己想干什么 以题注说的是Java为主 那么起码要知道找一个Java开发的工作需要什么能力 基本
  • postgresql 中的COALESCE()函数使用小技巧

    这篇文章主要介绍了postgresql 中的COALESCE 函数使用小技巧 具有很好的参考价值 希望对大家有所帮助 一起跟随小编过来看看吧 场景 存在一个用户白名单表 提供了此用户的用户名和地区信息 判断此用户是否在此白名单表中 如 姓名
  • Spring Boot 获取接口调用者的IP

    需求 我们实现一个登陆功能时 可能会有需要记录登陆者IP的需求 用于系统安全分析或账户来源分析 当然还有更多应用场景 工具类如下 来源于网络 已兼容K8S 代理的情况 import org slf4j Logger import org s
  • 树莓派解决 vim 编辑器中文乱码问题

    目录 1 问题由来 2 乱码问题演示 3 解决方案 4 问题解决 1 问题由来 我们用树莓派做项目的时候经常需要在电脑编写程序代码 然后再把代码移植进树莓派进行运行 我们用电脑编写程序的时候避免不了都会写很多中文注释 当我们把 file c
  • 【BEV Review】论文 Delving into the Devils of Bird’s-eye-view 2022-9 笔记

    背景 一般来说 自动驾驶车辆的视觉传感器 比如摄像头 安装在车身上方或者车内后视镜上 无论哪个位置 摄像头所得到的都是真实世界在透视视图 Perspective View 下的投影 世界坐标系到图像坐标系 这种视图与人类的视觉系统很类似 因
  • 表单数据自动封装到javaBean中

    页面表单数据的自动封装到javaBean中 先定义一个Bean类 package com test public class Bean private String name private Integer sex public Strin