All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jodd.bean.loader.SessionBeanLoader Maven / Gradle / Ivy

There is a newer version: 3.4.1
Show newest version
// Copyright (c) 2003-2012, Jodd Team (jodd.org). All Rights Reserved.

package jodd.bean.loader;

import java.util.Enumeration;

import javax.servlet.http.HttpSession;

import jodd.util.StringUtil;

/**
 * Populates java bean from HttpSession objects. It allows to be instanced with a
 * 'prefix' that will be added in front of all attributes.
 */
public class SessionBeanLoader extends BaseBeanLoader {

	protected final String prefix;

	public SessionBeanLoader() {
		this.prefix = null;
	}

	public SessionBeanLoader(String prefix) {
		this.prefix = prefix;
	}

	public void load(Object bean, Object source) {
		if (source instanceof HttpSession) {
			HttpSession session = (HttpSession) source;

			Enumeration attributeNames = session.getAttributeNames();

			while (attributeNames.hasMoreElements()) {
				String attributeName = (String) attributeNames.nextElement();

				Object value = session.getAttribute(attributeName);

				if (prefix != null) {
					attributeName = prefix + StringUtil.capitalize(attributeName);
				}

				beanUtilBean.setPropertyForcedSilent(bean, attributeName, value);
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy