Child pages
  • Application, Session, Request objects in vm
Skip to end of metadata
Go to start of metadata
$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>
  • No labels

4 Comments

  1. Anonymous

    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.

  2. Anonymous

    This does not work. Should it?

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

    $stack.findValue("keyx")

  3. Anonymous

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

    $stack.findValue("#keyx")

  4. 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