Oh, yes. $request.session DOES create the session, my mistake, no need for $request.getSession(true)
Le mercredi 28 février 2007 à 14:31 -0800, Nathan Bubna a écrit : > Yes and no. Even if we fix the known issue, that won't necessarily > mean that a session will be created. The VelocityViewServlet is not > going to go creating sessions without some cause; i'll never let it do > that. I agree. That allows tests like #if($session). Claude > However, if there tools declared to be session-scoped in the > toolbox.xml, then it assumes that the user must always want those > tools and therefore, if no session has already been created, it > creates one to put the tools into. Even this can be turned off by > specifying <create-session>false</create-session> within the <toolbox> > tags of your toolbox.xml. > > So, if you want a session, then you will need to either add a tool > that is session-scoped (so the ServletToolboxManager will create a > session to store that tool in), or else create one yourself in a > filter or servlet of your liking, or else do as Claude suggests and > use $request.session in your templates. That will create a session > automatically if one does not yet exist: > > http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/http/HttpServletRequest.html#getSession() > > Using $session alone will not create a session for you if one does not > exist (which i like, because it allows me to test whether there is a > session or not). > > On 2/28/07, Claude Brisson <[EMAIL PROTECTED]> wrote: > > This is a known problem with the current version of VelocityTools: the > > session is not yet created at the time $session is initialized, and > > remains null afterwards even if the session is later created. > > > > Try using $request.session instead of just $session if you are sure that > > the session does exist, or event $request.getSession(true) -which should > > never fail- otherwise. > > > > > > Claude > > > > Le mercredi 28 février 2007 à 18:18 +0000, Townson, Chris a écrit : > > > Hi, > > > > > > We've been experiencing some slightly strange behaviour attempting to > > > access the session object in the velocity context (i.e. from templates) > > > using velocity tools 1.3. > > > > > > We kept finding that $session was null. > > > > > > As part of our attempts to debug the problem, we basically set-up a tiny > > > app which was an exact mimic of the simple.war example web app (where we > > > found that $session was working fine). > > > > > > The only difference between our minimal app and simple.war was that we > > > used velocity 1.5beta2 > > > > > > We continued to encounter problems in our own minimal application _until_ > > > we added a toolbox with a session scoped tool. > > > > > > This seems very strange as I believe $session should be available > > > regardless when using VelocityView? > > > > > > Has anyone got any ideas as to why we might be experiencing this problem? > > > > > > It was noticable that we could not replicate this effect in simple.war > > > (i.e. commenting/uncommenting the "map tool" there did not affect access > > > to $session) > > > > > > Below is the deployment descriptor, toolbox, and list of jars used to > > > create our copy of simple.war. > > > > > > Cheers, > > > > > > Chris > > > > > > -- web.xml -- > > > > > > <?xml version="1.0" encoding="utf-8"?> > > > <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application > > > 2.2//EN" > > > "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> > > > <web-app> > > > > > > <servlet> > > > <servlet-name>velocity</servlet-name> > > > > > > <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class> > > > </servlet> > > > > > > <servlet-mapping> > > > <servlet-name>velocity</servlet-name> > > > <url-pattern>*.vm</url-pattern> > > > </servlet-mapping> > > > > > > <welcome-file-list> > > > <welcome-file>index.vm</welcome-file> > > > </welcome-file-list> > > > > > > </web-app> > > > > > > > > > -- toolbox.xml -- > > > > > > <?xml version="1.0"?> > > > <toolbox> > > > <xhtml>true</xhtml> > > > <data type="number"> > > > <key>velocityVersion</key> > > > <value>1.5</value> > > > </data> > > > <data type="number"> > > > <key>velocityToolsVersion</key> > > > <value>1.3</value> > > > </data> > > > <data type="boolean"> > > > <key>booleanTestValue</key> > > > <value>true</value> > > > </data> > > > <data type="string"> > > > <key>applicationName</key> > > > <value>Lenin 0.1-alpha</value> > > > </data> > > > <!-- > > > If you comment/uncomment the map tool, > > > access to $session should toggle off/on > > > --> > > > <tool> > > > <key>map</key> > > > <scope>session</scope> > > > <class>java.util.HashMap</class> > > > </tool> > > > <tool> > > > <key>date</key> > > > <scope>application</scope> > > > <class>org.apache.velocity.tools.generic.DateTool</class> > > > </tool> > > > </toolbox> > > > > > > -- jars -- > > > > > > commons-beanutils-1.7.0 > > > commons-collections-3.2 > > > commons-digester-1.8 > > > commons-lang-2.2 > > > commons-logging-1.1 > > > oro-2.0.8 > > > velocity-1.5beta2 > > > velocity-tools-view-1.3 > > > > > > ******************************************************************************** > > > DISCLAIMER: This e-mail is confidential and should not be used by anyone > > > who is > > > not the original intended recipient. If you have received this e-mail in > > > error > > > please inform the sender and delete it from your mailbox or any other > > > storage > > > mechanism. Neither Macmillan Publishers Limited nor any of its agents > > > accept > > > liability for any statements made which are clearly the sender's own and > > > not > > > expressly made on behalf of Macmillan Publishers Limited or one of its > > > agents. > > > Please note that neither Macmillan Publishers Limited nor any of its > > > agents > > > accept any responsibility for viruses that may be contained in this > > > e-mail or > > > its attachments and it is your responsibility to scan the e-mail and > > > attachments (if any). No contracts may be concluded on behalf of Macmillan > > > Publishers Limited or its agents by means of e-mail communication. > > > Macmillan > > > Publishers Limited Registered in England and Wales with registered number > > > 785998 > > > Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS > > > ******************************************************************************** > > > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]