• BeanUtils简单应用


    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0">
      
      
      <servlet>
          <servlet-name>servlet</servlet-name>
          <servlet-class>zr.com.servlet.TestServlet</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>servlet</servlet-name>
          <url-pattern>*.lf</url-pattern>
      </servlet-mapping>
    
    </web-app>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        
        <title>test BeanUtils</title>
        
      </head>
      
      <body>
        
        <form action="BeanUtils/lf.lf" method="post">
            
            品牌:<input name="brand" /><br>
            颜色:<input name="color" /><br>
            车龄:<input name="carAge" /><br>
            生成日期:<input name="createDate" /><br>
            <input  type="submit" value="提交"/>
        </form>
    
    
      </body>
    </html>
    package zr.com.pojo;
    
    import java.sql.Date;
    
    /**
     * 
     * @author LF
     *
     */
    public class Car {
    
        private String brand;
        
        private String color;
        
        private int carAge;
        
        private Date createDate;
    
        public String getBrand() {
            return brand;
        }
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        public int getCarAge() {
            return carAge;
        }
    
        public void setCarAge(int carAge) {
            this.carAge = carAge;
        }
    
        public Date getCreateDate() {
            return createDate;
        }
    
        public void setCreateDate(Date createDate) {
            this.createDate = createDate;
        }
    
        public Car() {
            super();
        }
    
        public Car(String brand, String color, int carAge, Date createDate) {
            super();
            this.brand = brand;
            this.color = color;
            this.carAge = carAge;
            this.createDate = createDate;
        }
    
        @Override
        public String toString() {
            return "Car [brand=" + brand + ", color=" + color + ", carAge="
                    + carAge + ", createDate=" + createDate + "]";
        }
        
        
    }
    package zr.com.servlet;
    
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    
    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 zr.com.pojo.Car;
    
    public class TestServlet extends HttpServlet{
        
        @Override
        public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            // 获取参数
            String brand = req.getParameter("brand");
            String color = req.getParameter("color");
            String carAge = req.getParameter("carAge");
            String createDate = req.getParameter("createDate");
            System.out.println(brand+","+color+","+carAge+","+createDate);
    
            Car car = new Car();
            
            try {
                //BeanUtils会自动转化类型
                BeanUtils.setProperty(car, "brand", brand);
                BeanUtils.setProperty(car, "color", color);
                BeanUtils.setProperty(car, "carAge", carAge);
                BeanUtils.setProperty(car, "createDate", createDate);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            
            System.out.println(car);
            
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
    
        }
        
        
    }
  • 相关阅读:
    java正则表达式验证
    sql之left join、right join、inner join的区别
    Servlet 输出图片验证码
    Myeclipse不能使用alt+/快捷方式的解决方法
    两个递增数据组合成一个递增数据(不适用)
    java回调机制
    java中的继承问题
    oracle自定义函数:将字符串中的特殊字符转化为逗号
    oracle常用的函数
    oracle中导入dmp数据注意事项
  • 原文地址:https://www.cnblogs.com/lantu1989/p/6706942.html
Copyright © 2020-2023  润新知