简体   繁体   中英

How can I make Apache velocity application session aware?

I am trying to use session data in a Velocity Servlet. I am debugging the servlet with Eclipse. The servlet seems to work (with the exception of the message change), but the name variable is not retained in the context across the conversation. Am I storing the session data in the wrong place? Or do I have a configuration problem?

HelloWorld.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;
public class HelloWorld extends VelocityViewServlet
{
    private static final String HELLO = "Hello! World!";
    private static final String GOODBYE = "Goodbye! Columbus!";
    private static final String NAME = "name";
    protected Template handleRequest(HttpServletRequest request,
            HttpServletResponse response, Context ctx)
    {
            Template template = null;
        try {template = getTemplate("hello.vm");}
        catch (Exception e) {throw new RuntimeException(e);}
        String name = (String)ctx.get(NAME);
        if (HELLO.equals(name))
            name = GOODBYE;
        else
            name = HELLO;
        ctx.put(NAME, name);
        return template;
    }
}

hello.vm

<html>
    <head>
        <script>
            function goodbye()
            {
                var form = document.createElement('form');
                form.setAttribute('method', 'post');
                form.setAttribute('action', '/HelloWorld?x=yes');
                form.style.display = 'hidden';
                document.body.appendChild(form);
                form.submit();
            }
        </script>
    </head>
    <body>
        <h4>$name</4>
        <button type="submit" onclick="goodbye()">Good-bye</button>
    </body>
</html>

tools.xml

<?xml version="1.0"?>
<tools>
    <toolbox scope="session">
        <tool key="hello" class="HelloWorld"/>
    </toolbox>
</tools>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>serv</servlet-name>
        <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>
        <init-param>
            <param-name>org.apache.velocity.toolbox</param-name>
            <param-value>/WEB-INF/tools.xml</param-value>
        </init-param>
        <init-param>
            <param-name>org.apache.velocity.properties</param-name>
            <param-value>/WEB-INF/velocity.properties</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>serv</servlet-name>
        <url-pattern>*.vm</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

You are putting an object of class HelloWorld inside your toolbox. But HellowWorld is a servlet! The toolbox is meant to only contains basic plain old java objects with getters and setters.

What you need to do is to use your servlet instead of the VelocityViewServlet, in the servlet-class tag in web.xml.

Then, I don't know exactly what you intend to do, but inside a template, you can reference your session with $session.

If your session has a 'name' attribute, then $name will return this attribute (provided it has not been overloaded with a value in a nested smaller scope, the request scope or the context itself).

With recent versions of Velocity/VelocityTools, you can use $session.name to get or set the name attribute. With older ones, you still need to use $session.setAttribute('name','value') to set the attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM