Back Forum Reply New

fromSession amp; session-scope contructor

Hello.. please help me any one.
I have a bean in WebApplicationContext.

so.. bean defind in session-scope. I whant to put this bean in fromSession for any other instance can access it during this from session.

The good thind is to put it ONCE. in time of creation in constructor.

Quesin: how can I obtain fromSession object in constructor?
Does ApplicationContext allow this?
of course a can put it in  handleRequest method. but this isn't right...b/c Bean Foo must persists in session immediately after creation. But not if some time
handleRequest will be called..

lt;bean id=quot;...quot; class=quot;Fooquot; scope=quot;sesionquot;gt;
lt;constructor-arg  ????? /gt;
lt;/beangt;

class Foo implements Controller{

public Foo(....){
//put this in fromsession
}

ModelAndView handleRequest(fromServletRequest request, fromServletResponse response) throws Exception{
   request.getSession().setAttribute(getClass().getNa  me()+quot;.FOOquot;, this); //NOT right!!!

}
}thanks..

Is it a Controller class you want to store in fromSession?

I believe a Controller must be thread-safe and is used by different requests from different sessions, and can not be session-aware, am I right?

and any session scope bean is put in the session when it is created and there is no need to do this stuff in the constructor.


Originally Posted by pashaIs it a Controller class you want to store in fromSession?

I believe a Controller must be thread-safe and is used by different requests from different sessions, and can not be session-aware, am I right?

and any session scope bean is put in the session when it is created and there is no need to do this stuff in the constructor.

1) yes, my class implements org..web.servlet.mvc.Controller  

2) I whant to have for each session unique controller

3) spring stores session scoped bean in session with attribute named quot;scopedTarget.[beanId] quot;
but I need to store some other objects in session in time session-bean creation. (when bean is created)
in constructor or init method

You definitely dont want to store your controller in the session ! Unless you have very good reasons to do so (in that case, I'd be very interested to hear them !). What you probably want, is to have your controller access the session to save its state ... but not to have any state on its own ...

Could you please tell us a bit more about what you are trying to achieve ? This way, we might be able to give you a reasonable answer.

I use tiles2 famework.
my tiles config looks likePHP Code:
lt;definitionnbsp;name="rf.custom.content"nbsp;template="page.jsp"gt;lt;put-attributenbsp;name="main.body"nbsp;type="object"nbsp;value="RFOBJECT_TILES_PREFIX_CUSTOM_CONTENT"/gt;lt;/definitiongt;

Tiles has 4 type of definition string, template, definition and object

according org.apache.tiles.impl.BasicTilesContainer, tiles has no way to render OJECT type definition. se code listing..PHP Code:
publicnbsp;voidnbsp;render(Attributenbsp;attr,nbsp;Writernbsp;writer,nbsp;Object...nbsp;requestItems)nbsp;{...........................switchnbsp;(type)nbsp;{nbsp;casenbsp;OBJECT:nbsp;thrownbsp;newnbsp;TilesException(nbsp;"Cannotnbsp;insertnbsp;annbsp;attributenbsp;ofnbsp;'object'nbsp;type");casenbsp;STRING:nbsp;writer.write(attr.getValue().toString());break;casenbsp;DEFINITION:nbsp;render(request,nbsp;attr.getValue().toString());break;casenbsp;TEMPLATE:nbsp;request.dispatch(attr.getValue().toString());break;default:nbsp;//nbsp;shouldnbsp;notnbsp;happennbsp;thrownbsp;newnbsp;TilesException(nbsp;"Unrecognizednbsp;typenbsp;fornbsp;attributenbsp;valuenbsp;"nbsp;+nbsp;attr.getValue());

I whant to implement my own rendering mechanism.
I store in session my own object, implementaion of following interface : PHP Code:
publicnbsp;interfacenbsp;RFObjectnbsp;{nbsp;/**nbsp;*nbsp;So...nbsp;mainnbsp;function.nbsp;Rendernbsp;context.nbsp;*nbsp;It'snbsp;callednbsp;bynbsp;{@linknbsp;ru.rusfinance.tiles.RFTilesContainernbsp;RFTilesContainer}nbsp;*nbsp;duringnbsp;renderingnbsp;process.nbsp;*nbsp;@paramnbsp;requestnbsp;fromServletRequestnbsp;*nbsp;@paramnbsp;responsenbsp;fromServletResponsenbsp;*nbsp;@throwsnbsp;Exceptionnbsp;ifnbsp;anynbsp;exceptionnbsp;accurs.nbsp;*/nbsp;publicnbsp;voidnbsp;render(fromServletRequestnbsp;request,nbsp;fromServletResponsenbsp;response)nbsp;throwsnbsp;Exception;}nbsp;

As you can see it has only one method.. render !

then I ovrrwrite TilesContener like

PHP Code:
publicnbsp;voidnbsp;render(Attributenbsp;attr,nbsp;Writernbsp;writer,nbsp;Object...nbsp;requestItems)nbsp;throwsnbsp;TilesException,nbsp;IOExceptionnbsp;{nbsp;switchnbsp;(attr.getType())nbsp;{nbsp;casenbsp;OBJECT:nbsp;//nbsp;getnbsp;fromnbsp;sessionnbsp;RFObjectnbsp;rfObject.render(request,response);break;}nbsp;default:nbsp;super.render(attr,nbsp;writer,nbsp;requestItems);}nbsp;}nbsp;

when tiles engine meet in jsp string likelt;spangt;lt;tiles:insertAttribute name=quot;main.bodyquot;/gt;lt;/spangt;
it gets my RFObject from fromSession and render it. (in my case this is a smal meny block in the head of page).

This RFObject must be placed in session ONCE!! during constructor creation.

Than's why I whant to have fromSession object in my controller bean contructor. (in constructor or init method)

everyone thanks.. sorry for my bad english.

I still don't see your usecase. If you make your bean session-scope (i.e. scope=quot;sessionquot;) your bean will be registered only once and at session creation time. So I still don't see the need for your hack...

The bean is already registered in your session, your hack would register it twice.

You are making it way to complex imho...

You already have a session scoped bean, use it, don't try to hack around it and register it again or register another bean. Simply use the already session scoped bean...Code:
lt;bean id=quot;yourRFObjectBeanquot; class=quot;RFObjectImplquot; scope=quot;sessionquot;gt; lt;property name=quot;somepropertyquot; value=quot;somevaluequot; /gt;
lt;/beangt;
¥
Back Forum Reply New