answers below

On Thu, Aug 28, 2008 at 12:57 AM, Gil Vernik <[EMAIL PROTECTED]> wrote:
> Hello everyone,
>
> I am trying to write a class that will read some values from properties
> file into array and then store this array in the user session.
>
> I want to do it using toolbox.xml,
>
> I created an entry
>                <tool>
>            <key>localeData</key>
>            <scope>session</scope>
>            <class>com.test.LocaleDataReader</class>
>      </tool>

make sure you don't have <create-session>false</create-session> in
your toolbox.xml, or your session tool will only be created if some
other part of the app creates a session instance.

> And the class LocaleDataReader is
>
> public class LocaleDataReader {
>
>    public void init(Object obj)
>    {
>        HttpServletRequest request = ((ViewContext)obj).getRequest();

// it doesn't look like you're doing anything with the request...
// do you need a request object for some reason?  if so, are you sure
// you want your tool created only once per session?

>    }
>
> public LocaleDataReader(){}
>
>    public ArrayList<String> getCurrentABC(){
>            ArrayList<String> currentABCtmp = new ArrayList<String>();
> // will read locale properties file, for now only
>            currentABCtmp.add("value");
>      return currentABCtmp;
>    }
>
> }
>
> Here is the question :
>
> A.  How can I store currentABCtmp variable into the session, so next
> time the call getCurrentABC() will return value from session. How can I
> access the session from this Method.

If your whole tool is session-scoped, then it is only created at most
once per session.  So, why not just store currentABCtmp in the tool?
If you really want to store it in the session for some reason, then in
your init(Object) method, you will have to ask the HttpServletRequest
for the HttpSession instance and either keep that in the tool or
immediately put the value into its attributes during init.

> B.  Why init() is never called? Who should call it? I saw init() in the
> source of some tools, I thought that how I can get access to session.

Well, first, init() should only be called once per session for a
session tool. So don't expect to see init() called on every request.
Second, what version of VelocityTools are you using?  Older versions
require a marker interface (ViewTool) to indicate that an init method
is present.  If it's a newer version, then it should work as-is.

> C.  I don't see any difference using debug between request and session
> scope of the entry in toolbox.xml, why is it?

using what debug?  and the difference would only be noticeable upon
subsequent requests, not the first request.

>
>
> Thanks a lot,
>
> Gil.
>
>
>
>
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to