Resolves the Viewtool interface in velocity-tools-2.0

Source: Internet
Author: User
Tags deprecated

For this issue please refer to:

http://www.oschina.net/question/100267_20922


Turn the content on the post and add your own solution.


Tsl0922 said: "

Beginner velocity, want to write a Velocitytool class implementation to embed dynamic pages in a relative path, the idea is roughly the same as in http://www.oschina.net/code/snippet_12_694, because the new version of the Init (Object ARG0) method has been Deprecated , swap with configure (MAP props) (see http://velocity.apache.org/tools/devel/ Upgrading.html and http://velocity.apache.org/tools/devel/creatingtools.html), the code is as follows:

Import Java.util.Map;
	 
Import Org.apache.velocity.VelocityContext;
Import Org.apache.velocity.tools.view.ViewContext;
	 
	public class Velocitytool {
	 
	    protected velocitycontext velocity;
	 
	    public void Configure (Map props) {
	        Object obj = Props.get ("Velocitycontext");
	        if (obj instanceof viewcontext) {
	            ViewContext viewcontext = (viewcontext) obj;
	            Velocity = (velocitycontext) viewcontext.getvelocitycontext ();
	        }
	    }
	 
	    Public String Cur_templete () {
	        return velocity.getcurrenttemplatename ();
	    }
	 
	}

After testing

public class Velocitytools {
	private viewcontext context;

	protected httpservletrequest request;

	Public Velocitytools () {
		System.out.println ("sssssssssssssssssssssssssssssssssssss");
	}

	public void Configure (map<?,? > Props) {
		Object obj = Props.get ("Velocitycontext");
		if (obj instanceof viewcontext) {
			context = (viewcontext) obj;
			Request = Context.getrequest ();
		}

	}

	public void init (Object arg0) {
		System.out.println ("Init");
		Scope:request or Session
		if (arg0 instanceof viewcontext) {
			System.out.println (" VVVVVVVVVVVVVVVCCCCCCCCCCCCCCCCCCCC ");
		}
		Scope:application
		Else if (arg0 instanceof ServletContext) {
			System.out.println (" SSSSSSSSSSSSSSSSSSSSCCCCCCCCCCCCCCCCCCCC ");}}}

Although the Viewtool interface is removed, the method can be added:

is automatically executed.


The following code in the Apache official website has the explanation, the English is not good, the single careful look will understand.

Links to related issues when connecting

Http://velocity.apache.org/tools/devel/creatingtools.html

Copy the Apache online Word.

If you want-to-allow your tool-to-be configured and you have both options:add a public setter for the property add a public void Configure (Map props) method you can, of course, does both, but if you do so keep in mind that the specific setters would Be called before the Configure () method. The application of configuration properties to specific setters are done using org.apache.commons.beanutils.PropertyUtils f Rom the commons-beanutils project, so the rules follow the typical beans property conventions.

Public Velocitytools () {
		System.out.println ("Do constructor Velocitytools");
	}

	public void Configure (map<?,? > Props) {
		Object obj = Props.get ("Velocitycontext");
		if (obj instanceof viewcontext) {
			context = (viewcontext) obj;
			Request = Context.getrequest ();
		}

	}
The instance being taken is a chainedcontext type, but he integrates the ViewContext directly with

obj instanceof ViewContext
You can do it. This will not be an error.

Properties: Deprecationsupportmode default is the Ture value.

The method Setdeprecationsupportmode (Boolean support) does not approve the use of the

The type of context created in the Createcontext () method must be Chainedcontext

Velocityview.java

...    private Boolean deprecationsupportmode = true; ...   @Deprecated     protected final void Setdeprecationsupportmode (Boolean support)   & nbsp {        if (deprecationsupportmode! = support)          {            This.deprecationsupportmode =
Support             Debug ("Deprecationsupportmode is now%s", ( Support?
"On": "Off"));        }    } .../** * <p>creates and returns an Initi alized Velocity context.</p> * * A New context of class {@link Viewtoolcontext} is created and * init
     Ialized. 
    * * @param request servlet request from client * @param response Servlets reponse to client */@Override Public Viewtoolcontext Createcontext (httpservletrequesT request, httpservletresponse response) {Viewtoolcontext ctx; if (this.deprecationsupportmode) {ctx = new Chainedcontext (velocity, request, response, Servletco
        ntext);
        } else {ctx = new Viewtoolcontext (velocity, request, response, ServletContext);
        } preparecontext (CTX, request);
    return CTX; }
Use reflection in Oldtoolinfo.java to get the Init method exists

Protected Method Getinit ()
    {
        if (this.init = = null)
        {
            try
            {
                //try to get a init (Object) method< C6/>this.init =
                    gettoolclass (). GetMethod ("Init", New class[]{Object.class});
            catch (nosuchmethodexception nsme)
            {
                //Ignore
            }
        }
        return this.init;
    }


Attach a code Listing

Package org.apache.velocity.tools;  /* * Licensed to the Apache software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * Distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * under the Apache License, Version 2.0 (The * "License");  You are not a use of this file except in compliance * with the License. Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applic  Able law or agreed to in writing, * software distributed under the License are distributed on A * "As is" BASIS, without  Warranties or CONDITIONS of any * KIND, either express OR implied.
 See the License for the * Specific language governing permissions and limitations * under the License.
*/import Java.lang.reflect.Method;
Import Java.util.Map;

Import Org.apache.velocity.tools.ToolContext; /** * manages old tools which still use the DeprecatEd Init () method. * * @author Nathan Bubna * @version $Id: Oldtoolinfo.java 674 2012-05-30 05:11:51z Liu_peng $ */public class Oldtoolin
    FO extends Toolinfo {private static final long serialversionuid = -4062162635847288761l;

    public static final String init_method_name = "INIT";

    private transient Method init;
     /** * Creates a new instance using the minimum required info * necessary for a tool.
    */Public Oldtoolinfo (String key, Class Clazz) {super (key, Clazz);
                } protected Method Getinit () {if (This.init = = null) {try {  Try to get an init (Object) method this.init = Gettoolclass (). GetMethod ("Init",
            New class[]{Object.class}); } catch (Nosuchmethodexception nsme) {//Ignore}} R
    Eturn This.init; }/** * * * @param clazz the Java.lang.Class of the tool */@Override public void SetClass (Class clazz) {Super.setclass (CLA

        ZZ);
    Clear any existing init method this.init = null; } @Override protected void Configure (Object tool, map<string,object> configuration) {//has

        Specific setters and configure (MAP) called First super.configure (tool, configuration);
        Method init = Getinit (); if (init! = null) {//CTX should, in all cases where a tool has such a method,//Actuall Y is a View (Tool) Context, but we don ' t want to link//To this class here, so as not to pollute the generic Ja
            R Object ctx = Configuration.get (Toolcontext.context_key);
            if (CTX! = null) {Invoke (init, tool, CTX);
 }
        }
    }

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.