[ 
https://issues.apache.org/struts/browse/WW-2796?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=44710#action_44710
 ] 

Flavien Binet commented on WW-2796:
-----------------------------------

some update, i managed to get it working with velocity-tools-1.4 :

I subclassed the VelocityManager class, overriden the createContextMethod, and 
replaced the deprecated constructor by the new one :

MyVelocityManager.java :
--------------------------------------------------------------------------------------
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.VelocityStrutsUtil;
import org.apache.struts2.views.jsp.ui.OgnlTool;
import org.apache.struts2.views.util.ContextUtil;
import org.apache.struts2.views.velocity.StrutsVelocityContext;
import org.apache.struts2.views.velocity.VelocityManager;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.context.ChainedContext;

import com.opensymphony.xwork2.util.ValueStack;

public class MyVelocityManager extends VelocityManager
{

            private static final Logger LOG = 
Logger.getLogger(MyVelocityManager.class);

            public Context createContext(ValueStack stack, HttpServletRequest 
req, HttpServletResponse res) {
                VelocityContext[] chainedContexts = prepareChainedContexts(req, 
res, stack.getContext());
                StrutsVelocityContext context = new 
StrutsVelocityContext(chainedContexts, stack);
                Map standardMap = ContextUtil.getStandardContext(stack, req, 
res);
                for (Iterator iterator = standardMap.entrySet().iterator(); 
iterator.hasNext();) {
                    Map.Entry entry = (Map.Entry) iterator.next();
                    context.put((String) entry.getKey(), entry.getValue());
                }
        context.put(STRUTS, new VelocityStrutsUtil(getVelocityEngine(), 
context, stack, req, res));

                ServletContext ctx = null;
                try {
                    ctx = ServletActionContext.getServletContext();
                } catch (NullPointerException npe) {
                    // in case this was used outside the lifecycle of struts 
servlet
                    LOG.debug("internal toolbox context ignored");
                }

                if (toolboxManager != null && ctx != null) {

                    // here is the new constructor :
                    ChainedContext chained = new 
ChainedContext(getVelocityEngine(), req, res, ctx);

                    chained.setToolbox(toolboxManager.getToolbox(chained));
                    return chained;
                } else {
                    return context;
                }

            }
}
--------------------------------------------------------------------------------------

Now just add this line :

struts.xml :
--------------------------------------------------------------------------------------
<constant name="struts.velocity.manager.classname" 
value="your.package.MyVelocityManager" />
--------------------------------------------------------------------------------------

and struts will use your new manager instead of the build in one.
Tested with velocity-tools-1.4 only, it seems to work fine.

Hope this help ;-) 

> org.apache.struts2.views.velocity.VelocityManager calling deprecated 
> velocity's ChainedContext constructor
> ----------------------------------------------------------------------------------------------------------
>
>                 Key: WW-2796
>                 URL: https://issues.apache.org/struts/browse/WW-2796
>             Project: Struts 2
>          Issue Type: Bug
>    Affects Versions: 2.0.11
>         Environment: velocity-tools-1.4.jar
> velocity-1.5.jar
> struts2.0.11.jar
>            Reporter: Flavien Binet
>            Priority: Critical
>
> Hello :)
> I'm facing a bug when trying to setup velocity's toolbox (version 1.4). 
> Here is the stack trace : 
> ----------------------------
> java.lang.NoSuchMethodError: 
> org.apache.velocity.tools.view.context.ChainedContext.<init>(Lorg/apache/velocity/context/Context;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;Ljavax/servlet/ServletContext;)V
>         
> org.apache.struts2.views.velocity.VelocityManager.createContext(VelocityManager.java:193)
>         
> org.apache.struts2.dispatcher.VelocityResult.createContext(VelocityResult.java:235)
>         
> org.apache.struts2.dispatcher.VelocityResult.doExecute(VelocityResult.java:152)
>         
> org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:178)
>         
> com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
>         
> com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
>         ... 
> ----------------------------
> VelocityManager object is trying to call a constructor that has been 
> deprecated since velocity-tools-1.3 (regarding javadoc -> 
> http://velocity.apache.org/tools/releases/1.3/javadoc/org/apache/velocity/tools/view/context/ChainedContext.html#ChainedContext(org.apache.velocity.context.Context,%20javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse,%20javax.servlet.ServletContext)
> It work well using velocity-tools-1.2.jar, but i need to use it under 1.4. 
> This new constructor seems to require a VelocityEngine instance that is not 
> available around :)
> Thank for support

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to