Results 1 to 4 of 4

Thread: change property value according to httpsession attribute

  1. #1

    Default change property value according to httpsession attribute

    I want to change a property of a class according to a attribute of httpsession. can somebody give a example? thanks

    for example:
    class A {
    private String x;

    public void setX(String x) {
    this.x = x;
    }
    }

    a attribute named B;

    if (B=='1')
    set A.x to '111111'
    if(B=='2')
    set A.x to '2222'

    can somebody give me a example?
    thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    If I understood your need, you want to set an object attribute value based on a HttpSession attribute value. If the value may change on each request, you can achieve this using a javax.servlet.Filter or a org.springframework.web.servlet.HandlerInterceptor .
    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3

    Default

    Could you show me a example? thanks

    I implements a class such as logger for hibernate.
    I want to set the property "username" on runtime.
    how to?


    Code:
    	<bean id="hibernateLoggerInterceptorImpl" class="creatxr.util.orm.hibernate.LoggerInterceptorImpl">
    	</bean>

    Code:
    public class LoggerInterceptorImpl implements Interceptor, Serializable &#123;
    	private static final Log logger = LogFactory
    			.getLog&#40;LoggerInterceptorImpl.class&#41;;
    
    	private final Map histories = new HashMap&#40;&#41;;
    
    	private String username;
    
    	public LoggerInterceptorImpl&#40;String username&#41; &#123;
    		super&#40;&#41;;
    		this.username = username;
    	&#125;
    
    	public LoggerInterceptorImpl&#40;&#41; &#123;
    		super&#40;&#41;;
    	&#125;
    
    
    	/* &#40;non-Javadoc&#41;
    	 * @see net.sf.hibernate.Interceptor#onLoad&#40;java.lang.Object, java.io.Serializable, java.lang.Object&#91;&#93;, java.lang.String&#91;&#93;, net.sf.hibernate.type.Type&#91;&#93;&#41;
    	 */
    	public boolean onLoad&#40;Object entity, Serializable id, Object&#91;&#93; state,
    			String&#91;&#93; propertyNames, Type&#91;&#93; types&#41; throws CallbackException &#123;
    		// TODO Auto-generated method stub
    		return false;
    	&#125;
    
    	/* &#40;non-Javadoc&#41;
    	 * @see net.sf.hibernate.Interceptor#onFlushDirty&#40;java.lang.Object, java.io.Serializable, java.lang.Object&#91;&#93;, java.lang.Object&#91;&#93;, java.lang.String&#91;&#93;, net.sf.hibernate.type.Type&#91;&#93;&#41;
    	 */
    	public boolean onFlushDirty&#40;Object entity, Serializable id,
    			Object&#91;&#93; currentState, Object&#91;&#93; previousState,
    			String&#91;&#93; propertyNames, Type&#91;&#93; types&#41; throws CallbackException &#123;
    
    		Map changedProps = new HashMap&#40;&#41;;
    		for &#40;int i=0; i<propertyNames.length; i++&#41; &#123;
    			Object newOne = currentState&#91;i&#93;;
    			Object oldOne = previousState&#91;i&#93;;
    
    			if &#40;&#40;newOne==null&#41;&&&#40;oldOne==null&#41;&#41; &#123;
    				continue;
    			&#125;
    
    			if &#40;newOne instanceof PersistentCollection&#41; &#123;
    //				Collections must be compared against the snapshot
    				PersistentCollection collection = &#40;PersistentCollection&#41; newOne;
    				if &#40;collection.isDirectlyAccessible&#40;&#41; == false&#41; &#123;
    					continue;
    				&#125;
    //				retrieve Snapshot
    				oldOne = collection.getCollectionSnapshot&#40;&#41;.getSnapshot&#40;&#41;;
    				if &#40;oldOne instanceof Map && newOne instanceof Set&#41; &#123;
    //				a Set is internally stored as Map
    					oldOne = &#40;&#40;Map&#41; oldOne&#41;.values&#40;&#41;;
    				&#125;
    			&#125;
    
    			if &#40;&#40;oldOne!=null&#41;&&&#40;oldOne.equals&#40;newOne&#41;&#41;&#41; &#123;
    				continue;
    			&#125;
    
    			Map changedProp = new HashMap&#40;&#41;;
    			changedProp.put&#40;"propertyName", propertyNames&#91;i&#93;&#41;;
    			changedProp.put&#40;"oldValue", oldOne&#41;;
    			changedProp.put&#40;"newValue", newOne&#41;;
    
    			changedProps.put&#40;"change", changedProp&#41;;
    		&#125;
    
    		System.out.println&#40;"onFlushDirty&#40;Object, Serializable, Object&#91;&#93;, Object&#91;&#93;, String&#91;&#93;, Type&#91;&#93;&#41;"
    					+ changedProps&#41;;
    		return false;
    	&#125;
    
    	/* &#40;non-Javadoc&#41;
    	 * @see net.sf.hibernate.Interceptor#onSave&#40;java.lang.Object, java.io.Serializable, java.lang.Object&#91;&#93;, java.lang.String&#91;&#93;, net.sf.hibernate.type.Type&#91;&#93;&#41;
    	 */
    	public boolean onSave&#40;Object entity, Serializable id, Object&#91;&#93; state,
    			String&#91;&#93; propertyNames, Type&#91;&#93; types&#41; throws CallbackException &#123;
    		// TODO Auto-generated method stub
    		System.out.println&#40;"onSave&#40;Object, Serializable, Object&#91;&#93;, String&#91;&#93;, Type&#91;&#93;&#41; - hibernate logger"&#41;;
    
    		return false;
    	&#125;
    
    	/* &#40;non-Javadoc&#41;
    	 * @see net.sf.hibernate.Interceptor#onDelete&#40;java.lang.Object, java.io.Serializable, java.lang.Object&#91;&#93;, java.lang.String&#91;&#93;, net.sf.hibernate.type.Type&#91;&#93;&#41;
    	 */
    	public void onDelete&#40;Object entity, Serializable id, Object&#91;&#93; state,
    			String&#91;&#93; propertyNames, Type&#91;&#93; types&#41; throws CallbackException &#123;
    		// TODO Auto-generated method stub
    
    	&#125;
    
    	/* &#40;non-Javadoc&#41;
    	 * @see net.sf.hibernate.Interceptor#preFlush&#40;java.util.Iterator&#41;
    	 */
    	public void preFlush&#40;Iterator entities&#41; throws CallbackException &#123;
    		// TODO Auto-generated method stub
    
    	&#125;
    
    	/* &#40;non-Javadoc&#41;
    	 * @see net.sf.hibernate.Interceptor#postFlush&#40;java.util.Iterator&#41;
    	 */
    	public void postFlush&#40;Iterator entities&#41; throws CallbackException &#123;
    		// TODO Auto-generated method stub
    
    	&#125;
    
    	/* &#40;non-Javadoc&#41;
    	 * @see net.sf.hibernate.Interceptor#isUnsaved&#40;java.lang.Object&#41;
    	 */
    	public Boolean isUnsaved&#40;Object entity&#41; &#123;
    		// TODO Auto-generated method stub
    		return null;
    	&#125;
    
    	/* &#40;non-Javadoc&#41;
    	 * @see net.sf.hibernate.Interceptor#findDirty&#40;java.lang.Object, java.io.Serializable, java.lang.Object&#91;&#93;, java.lang.Object&#91;&#93;, java.lang.String&#91;&#93;, net.sf.hibernate.type.Type&#91;&#93;&#41;
    	 */
    	public int&#91;&#93; findDirty&#40;Object entity, Serializable id,
    			Object&#91;&#93; currentState, Object&#91;&#93; previousState,
    			String&#91;&#93; propertyNames, Type&#91;&#93; types&#41; &#123;
    		// TODO Auto-generated method stub
    		return null;
    	&#125;
    
    	/* &#40;non-Javadoc&#41;
    	 * @see net.sf.hibernate.Interceptor#instantiate&#40;java.lang.Class, java.io.Serializable&#41;
    	 */
    	public Object instantiate&#40;Class clazz, Serializable id&#41;
    			throws CallbackException &#123;
    		// TODO Auto-generated method stub
    		return null;
    	&#125;
    
    
    	public String getUsername&#40;&#41; &#123;
    		return username;
    	&#125;
    
    	public void setUsername&#40;String username&#41; &#123;
    		this.username = username;
    	&#125;
    
    &#125;

  4. #4
    Join Date
    Jun 2005
    Posts
    6

    Default with the same doubt

    Hello creatxr,

    I am in the same predicament as you are. Have you found any viable solution? Anybody in the forum has an idea how to solve this one?

    Thank you all, best regards.
    Eduardo Hernández

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Unit testing with JOTM and JtaTransactionManager
    By lalle in forum Architecture
    Replies: 1
    Last Post: Oct 15th, 2005, 09:05 AM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Replies: 4
    Last Post: Aug 17th, 2005, 04:42 AM
  5. Replies: 2
    Last Post: May 13th, 2005, 05:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •