133-jsp动作标签与BeanUtils

44 篇文章 0 订阅
20 篇文章 0 订阅



jsp动作标签

首先,jsp动作标签和html的这些标签
是有本质的区别的

动作标签是由tomcat(服务器)来解释执行的
	html标签是由浏览器来执行的

所以其实动作标签就和java代码一样
都是在服务器端执行的




标签
<jsp:forward> 请求转发
就和RequestDispatcher的forward方法一样
一个是在Servlet中使用
一个是在jsp中使用

<jsp:include> 请求包含
一样的道理
这个也就是和RequestDispatcher的include方法一样
一个是在Servlet




Introspector 内省
内省其实就是通过反射来操作javabean
但是比使用反射要方便一点

我们可以看看Introspector的getBeanInfo方法

    /**
     * Introspect on a Java Bean and learn about all its properties, exposed
     * methods, and events.
     * <p>
     * If the BeanInfo class for a Java Bean has been previously Introspected
     * then the BeanInfo class is retrieved from the BeanInfo cache.
     *
     * @param beanClass  The bean class to be analyzed.
     * @return  A BeanInfo object describing the target bean.
     * @exception IntrospectionException if an exception occurs during
     *              introspection.
     * @see #flushCaches
     * @see #flushFromCaches
     */
    public static BeanInfo getBeanInfo(Class<?> beanClass)
        throws IntrospectionException
    {
        if (!ReflectUtil.isPackageAccessible(beanClass)) {
            return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
        }
        ThreadGroupContext context = ThreadGroupContext.getContext();
        BeanInfo beanInfo;
        synchronized (declaredMethodCache) {
            beanInfo = context.getBeanInfo(beanClass);
        }
        if (beanInfo == null) {
            beanInfo = new Introspector(beanClass, null, USE_ALL_BEANINFO).getBeanInfo();
            synchronized (declaredMethodCache) {
                context.putBeanInfo(beanClass, beanInfo);
            }
        }
        return beanInfo;
    }

翻译一下
对一个javabean进行内省,
然后可以获取它的属性,方法和事件
如果一个javabean的BeanInfo类已经内省过了
那么会从BeanInfo缓存中获取BeanInfo类



我们来看一下获取属性方法和事件的get方法

    public static void main(String[] args) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(User.class);
            BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
            EventSetDescriptor[] eventSetDescriptors = beanInfo.getEventSetDescriptors();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
    }



内省其实就是这样的一个过程
内省---Bean信息---属性描述符
属性的get/set方法---反射

我们可以使用commons包的BeanUtils
我们找到commons官网
网址:
http://commons.apache.org/proper/commons-beanutils

下载一个beanutils的jar包

注意了
我们要使用BeanUtils
还要用到另外一个commons的包
就是commons-logging
BeanUtils对这个包是有依赖的




commons官网
http://commons.apache.org/proper/
可以保存一下




我们来使用一下BeanUtils
我们从最基本的类名反射开始

    public static void main(String[] args) throws Exception {
        String className = "user.domain.User";
        Class<?> clazz = Class.forName(className);
        Object bean = clazz.newInstance();

        BeanUtils.setProperty(bean, "name", "abc001");
	BeanUtils.setProperty(bean, "xxx","xxx");
        System.out.println(bean.toString());
    }

这里我们设置了一个xxx属性为xxx
我们的bean类中并没有xxx属性
但是这样也不会报错
输出结果为
User{id=0, name='abc001', age=0, phone='null'}
没问题





现在我们来把一个map中的属性
直接封装到一个bean中

Map:{"name":"abc","age":"123"}

我们来写java代码
    public static void main(String[] args) throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "abc");
        map.put("age", "123");

        User user = new User();
        BeanUtils.populate(user, map);

        System.out.println(user.toString());
    }

先创建map,放入键值对
然后我们调用populate方法
把属性放到user对象里面
输出结果为:
User{id=0, name='abc', age=123, phone='null'}
成功





然后我们来写一个toBean方法

    public static <T> T toBean(Map map, Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            BeanUtils.populate(t, map);

            return t;
        } catch (Exception e) {
            return null;
        }
    }



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值