Dashboard > WebWork > ... > Cookbook > Application, Session, Request objects in vm
  WebWork Log In View a printable version of the current page.  
  Application, Session, Request objects in vm
Added by Bernard Choi, last edited by Jason Carreira on Nov 30, 2004  (view change)
Labels: 
(None)

$req.session.servletContext.getAttribute(...)
$req.session.getAttribute(...)
$req.getAttribute(...)

To get parameters from the QueryString or from a POSTed form, do not use getAttribute, use:

$req.getParameter(...)

But that's quite obvious, since $req is the request object and we all know how it works.

Example:

_test.jsp_:

<html><head></head><body>
<%
session.setAttribute("sessionFoo", "sessionBar");
session.getServletContext().setAttribute("applicationFoo", "applicationBar");
%>

<p>The following information should be available when sending the form below:

<ul>
	<li>Request parameter 'querystringFoo' with value 'querystringBar';</li>
	<li>Request parameter 'formFoo' with value 'formBar';</li>
	<li>Session attribute 'sessionFoo' with value 'sessionBar';</li>
	<li>Application attribute 'applicationFoo' with value 'applicationBar'.</li>
</ul>
</p>

<form action="test.vm?querystringFoo=querystringBar" method="post">
<input type="hidden" name="formFoo" value="formBar">
<p><input type="submit" value="Test!"></p>
</form>
</body></html>

_test.vm_:

<html><head></head><body>

#set ($ses = $req.getSession())
#set ($app = $ses.getServletContext())

<p>applicationFoo = $!app.getAttribute("applicationFoo") <code>(app.getAttribute("applicationFoo"))</code></p>
<p>sessionFoo = $!ses.getAttribute("sessionFoo") <code>(ses.getAttribute("sessionFoo"))</code></p>
<p>formFoo = $!req.getParameter("formFoo") <code>(req.getParameter("formFoo"))</code></p>
<p>querystringFoo = $!req.getParameter("querystringFoo") <code>(req.getParameter("queryStringFoo"))</code></p>

</body></html>

How do I set a request scoped object in an Interceptor and access it from a Velocity template?

I've tried every combination and find it too difficult to switch from Struts and Servlets to Webwork. The documentation could include a section which describes the application, session and request scopes in more detail. Three lines of documentation is not enough.

Posted by Anonymous at Dec 30, 2003 04:17 | Permalink

This does not work. Should it?

ActionContext.getContext().put("keyx", "valuex");

$stack.findValue("keyx")

Posted by Anonymous at Dec 30, 2003 04:30 | Permalink

Found the problem. I have to put # in front of the key.

$stack.findValue("#keyx")

Posted by Anonymous at Jan 01, 2004 10:48 | Permalink

From the JavaDocs of the class com.opensymphony.webwork.views.velocity.VelocityManager

This method is responsible for creating the standard VelocityContext used by all WW2 velocity views. The following context parameters are defined:

  • req - the current HttpServletRequest
  • res - the current HttpServletResponse
  • stack - the current OgnlValueStack
  • ognl - an OgnlTool
  • webwork - an instance of WebWorkUtil
  • action - the current WebWork action
Posted by Vitor Souza at Feb 12, 2004 07:36 | Permalink
Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators